我正在尝试为单例创建一个访问方法.我收到此错误(请参阅下面的代码).我不明白为什么我会收到此错误以及此错误的含义.谁能解释一下?
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
static private var thedelegate: AppDelegate?
class var delegate : AppDelegate {
return thedelegate!
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
thedelegate = self // syntax error: Static member 'thedelegate' cannot be used on instance of type 'AppDelegate'
Run Code Online (Sandbox Code Playgroud)
bjc*_*bjc 31
您需要在声明它的类名前面添加static/class变量或函数,即使在同一个类中也是如此.
在这种情况下,你想要 return AppDelegate.thedelegate!
而且,正如Martin R.指出的那样, AppDelegate.thedelegate = self
Sas*_*huk 23
您正尝试从该类的实例访问类级变量.要使用它,您需要创建类级别函数:static func().试试这个:
static func sharedDelegate() -> AppDelegate {
return UIApplication.sharedApplication().delegate as! AppDelegate
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33133 次 |
| 最近记录: |