Home > Mobile >  Class static member variables, why do you use an error
Class static member variables, why do you use an error

Time:09-24

 import Foundation 

Print (" Hello, World!" )

Public class wechat: NSObject {
The static var staticSelf: wechat?
Var a: Int=1;

Override init () {
Super. The init ()
Wechat. StaticSelf=self
}
Public func loginSucc () - & gt; Void {
//the error
Print (wechat. StaticSelf. A);

//ok
If let the controller=wechat. StaticSelf {
Print (controller. A);
}
Return
}
}


Why the error that part of the compiler error, here ok

CodePudding user response:

//
//main. Swift
//testCMD
//
//Created by LXLX - cx on 2020/1/14.
//Copyright? 2020 LXLX - cx. All rights reserved.
//

The import Foundation

Print (" Hello, World!" )

Public class wechat: NSObject {
The static var staticSelf: wechat {
Set (selfInstance) {
Wechat. StaticSelf=selfInstance
}
The get {
Return wechat. StaticSelf
}
}
Var a: Int=1;

Override init () {
Super. The init ()
Wechat. StaticSelf=self
}
Public func loginSucc () - & gt; Void {
//the error
Print (wechat. StaticSelf. A);

//ok
/*
If let the controller=wechat. StaticSelf {
Print (controller. A);
}
*/
Return
}
}



This modified to remove the optional type can also be compiled,
  • Related