简体   繁体   中英

Failable Init - Variable 'self.customerType' used before being initialized

I've read up and down regarding this, and understand the basics here - I just can't understand why I get this error. I use the second init to instantiate a customer from Firebase, but even if I comment out everything inside it, I still get the error

Variable 'self.customerType' used before being initialized at the declaration of init?

class Customer {

    enum customerTypes {
        case consumer
        case business
    }

    // MARK - properties

    var id: String?
    let customerType: customerTypes
    var name1: String
    var name2: String?
    var address: Address?
    var phone: String?
    var email: String?

    struct Address {
        var street: String
        var postalCode: String
        var city: String
        var country: String = "Norway"
    }

    init(type: customerTypes, name1: String, name2: String?, phone: String, email: String, address: Address? ) {
        self.customerType = type
        self.name1 = name1
        self.name2 = name2
        self.phone = phone
        self.email = email
        self.address = address
    }

     init?(data: [String: Any]) {

        guard let type = data["customerType"] as? String else { return }
        guard let name1 = data["name1"] as? String  else { return }

        self.customerType = type == "Consumer" ? .consumer : .business
        self.name1 = name1


//      if let name2 = data["name2"] as? String { self.name2 = name2 }
//      if let phone = data["phone"] as? String { self.phone = phone }
//      if let email = data["email"] as? String{ self.email = email }
//      if let address = data["address"] as? [String: Any] {
//          let street = address["street"] as? String
//          let postCode = address["postCode"] as? String
//          let city = address["city"] as? String
//          if street != nil && postCode != nil && city != nil {
//              self.address = Address(street: street!, postalCode: postCode!, city: city!)
//          }
//      }

    }


What simple issue am I overlooking here?

You declare an initializer which promises to either return an initialized Customer or no Customer (because it is fallible). You alo declare let customerType: customerTypes as one of the properties of the class.

That means that if you successfully return from the initializer (that means, not returning nil ), this property has to be initialized to some value.

The error is not very helpful in the location of the error, as the error is actually on the line below. By simply putting return in your guard , you are saying that your object is successfully initialized, which it is not, as you have not yet set customerType to a value.

So if you put a return nil in your guard clause, you will say that your initialization failed, and then you do not need to put a value in customerType .

The properties that don't have an initial value needs to set inside an init . You can fix the issue by either setting them as Optional or by setting a default value:

init?(data: [String: Any]) {
    customerType = .consumer
    name1 = ""
}

or:

var customerType: customerTypes?
var name1: String?

Note: By setting the properties Optional the compiler assumes that the initial value is nil .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM