我遇到了下一个问题:我必须定义可以在类方法中使用的变量。像这样:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
guard let splitViewController = window?.rootViewController as? UISplitViewController,
let leftNavController = splitViewController.viewControllers.first as? UINavigationController,
let masterViewController = leftNavController.topViewController as? MasterViewController,
let detailViewController = splitViewController.viewControllers.last as? DetailViewController
else { fatalError() }
let firstMonster = masterViewController.monsters.first
detailViewController.monster = firstMonster
return true
}
我尝试使用类属性,但是它对我不起作用。我相信存在一种在TypeScript中定义此类变量的方法。
答案 0 :(得分:1)
只需从变量中删除
let
,var
或const
。
并使用this
关键字引用它们。
class MyClass {
var1: string | number;
var2: number;
method1() {
this.var1 = this.var2;
}
method2() {
this.var1 = "something";
this.var2 = 32;
}
}