不能在属性初始值设定项中使用实例成员'server' [英] Cannot use instance member 'server' within property initializer

查看:845
本文介绍了不能在属性初始值设定项中使用实例成员'server'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:无法在属性初始值设定项中使用实例成员'服务器';属性初始化程序在我的代码的这一行中'自己可用'之前运行

I'm getting this error: "Cannot use instance member 'server' within property initializer; property initializers run before 'self's available" in this line of my code

编辑

   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.
    }

我如何解决它?错误发生在这一行

how i can solve it? The error is at this line

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


推荐答案

您不能使用视图控制器和属性的实例,直到初始化,所以你只需要将 ChinoAPI 初始化移动到 viewDidLoad

You cannot use an instance of your view controller and properties until the initialization, so you just need to move your ChinoAPI initialization toviewDidLoad:

var chino: ChinoAPI!

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

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

Another option is to move all hardcoded values from your view controller to ChinoAPI, but I'm not sure if it will fit your logic well.

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

Also, you can just move the values to init like:

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

这篇关于不能在属性初始值设定项中使用实例成员'server'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆