1

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

EDIT

   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)
bero
  • 201
  • 1
  • 2
  • 13

2 Answers2

1

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

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.

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

ChinoAPI.init(hostUrl: "https://api.test.chino.io/v1", customerId: "xxx", customerKey: "xxx")
jherran
  • 3,337
  • 8
  • 37
  • 54
Liubo
  • 674
  • 9
  • 18
0

You need to use self after view controller init method. You can init your var chino in viewDidLoad or need to use hardcodet string if you need to init it before init view controller init method