无法使用实例成员' server'在属性初始化程序中

时间:2017-06-20 08:57:16

标签: ios swift xcode

我收到此错误:"无法使用实例成员'服务器'在属性初始化器内;财产初始化程序在“自己可用”之前运行"在我的代码的这一行

修改

   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)

2 个答案:

答案 0 :(得分:1)

在初始化之前,您无法使用视图控制器和属性的实例,因此您只需将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")

答案 1 :(得分:0)

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