不能在属性初始值设定项中使用实例成员’server’

我收到此错误:“无法在属性初始值设定项中使用实例成员’服务器’;属性初始值设定项在我的代码行中’self’可用之前运行”

编辑

import UIKit import ChinoOSXLibrary class LoginCL: UIViewController { @IBOutlet weak var emailField: UITextField! @IBOutlet weak var passField: UITextField! var loggedUser: LoggedUser! var customerId = "xxx" var customerKey = "xxx" var server = "https://api.test.chino.io/v1" var chino = ChinoAPI.init(hostUrl: server, customerId: customerId, customerKey: customerKey) override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } 

我怎么能解决它? 错误就在这一行

 var chino = ChinoAPI.init(hostUrl: server, customerId: customerId, customerKey: customerKey) 

在初始化之前,您不能使用视图控制器和属性的实例,因此您只需将ChinoAPI初始化移动到viewDidLoad

 var chino: ChinoAPI! override func viewDidLoad() { super.viewDidLoad() chino = ChinoAPI(hostUrl: server, customerId: customerId, customerKey: customerKey) } 

另一种选择是将所有硬编码值从视图控制器移动到ChinoAPI ,但我不确定它是否适合您的逻辑。

此外,您可以将值移动到init,如:

 ChinoAPI.init(hostUrl: "https://api.test.chino.io/v1", customerId: "xxx, customerKey: "xxx") 

您需要在视图控制器init方法之后使用self。 您可以在viewDidLoad中初始化var chino,或者如果需要在init view controller init方法之前初始化它,则需要使用hardcodet字符串