使用便利初始化程序创建第二个可用的初始化程序

时间:2016-10-04 02:09:08

标签: swift initialization

我正在尝试创建第二个可用的便利初始化程序,因为我想要进行两次API调用,而第二个可用的初始化程序包含我从原始API获得的所有属性,这与第一个可用的初始化程序不同这将包含所有原始属性以及我将添加到firebase上的一些其他属性。

class Alcohol {
    var id: Int
    var companyName: String
    var address: String
    var city: String
    var state: String
    var postal: String
    var country: String
    var phone: String
    var email: String
    var url: String
    var checkedBy: String
    var notes: String
    var status: String
    var statusColor: String
    var identifier: NSUUID?
    var barnivoreChecked: Bool?
    var alcoholType: AlcoholType?

    // This failable init is for the information I'll be retrieving from firebase.

    init?(dictionary: [String: AnyObject], type: AlcoholType) {
        guard
            let id = dictionary[Constants.kID] as? Int,
            let companyName = dictionary[Constants.kCompanyName] as? String,
            let address = dictionary[Constants.kAddress] as? String,
            let city = dictionary[Constants.kCity] as? String,
            let state = dictionary[Constants.kState] as? String,
            let postal = dictionary[Constants.kPostal] as? String,
            let country = dictionary[Constants.kCountry] as? String,
            let phone = dictionary[Constants.kPhone] as? String,
            let email = dictionary[Constants.kEmail] as? String,
            let url = dictionary[Constants.kURL] as? String,
            let checkedBy = dictionary[Constants.kCheckedBy] as? String,
            let notes = dictionary[Constants.kNotes] as? String,
            let status = dictionary[Constants.kStatus] as? String,
            let statusColor = dictionary[Constants.kStatusColor] as? String,
            let barnivoreChecked = dictionary[Constants.kBarnivoreChecked] as? Bool else { return nil }

        self.id = id
        self.companyName = companyName
        self.address = address
        self.city = city
        self.state = state
        self.postal = postal
        self.country = country
        self.phone = phone
        self.email = email
        self.url = url
        self.checkedBy = checkedBy
        self.notes = notes
        self.status = status
        self.statusColor = statusColor
        self.barnivoreChecked = barnivoreChecked
        self.alcoholType = type
    }

    // This failable initializer has all the original properties I want to get from the initial API which I'll be retrieving the information from.

    convenience init?(barnivoreDictionary: [String: AnyObject]) {
        guard
            let id = barnivoreDictionary[Constants.kID] as? Int,
            let companyName = barnivoreDictionary[Constants.kCompanyName] as? String,
            let address = barnivoreDictionary[Constants.kAddress] as? String,
            let city = barnivoreDictionary[Constants.kCity] as? String,
            let state = barnivoreDictionary[Constants.kState] as? String,
            let postal = barnivoreDictionary[Constants.kPostal] as? String,
            let country = barnivoreDictionary[Constants.kCountry] as? String,
            let phone = barnivoreDictionary[Constants.kPhone] as? String,
            let email = barnivoreDictionary[Constants.kEmail] as? String,
            let url = barnivoreDictionary[Constants.kURL] as? String,
            let checkedBy = barnivoreDictionary[Constants.kCheckedBy] as? String,
            let notes = barnivoreDictionary[Constants.kNotes] as? String,
            let status = barnivoreDictionary[Constants.kStatus] as? String,
            let statusColor = barnivoreDictionary[Constants.kStatusColor] as? String else {
                self.init(id: 0, companyName: "", address: "", city: "", state: "", postal: "", country: "", phone: "", email: "", url: "", checkedBy: "", notes: "", status: "", statusColor: "", alcoholType: alcoholType! )
                return nil
        }

        self.id = id
        self.companyName = companyName
        self.address = address
        self.city = city
        self.state = state
        self.postal = postal
        self.country = country
        self.phone = phone
        self.email = email
        self.url = url
        self.checkedBy = checkedBy
        self.notes = notes
        self.status = status
        self.statusColor = statusColor
        self.alcoholType = nil
    }

    init(id: Int, companyName: String, address: String, city: String, state: String, postal: String, country: String, phone: String, email: String, url: String, checkedBy:String, notes: String, status: String, statusColor: String, barnivoreChecked: Bool = true, alcoholType: AlcoholType) {

        self.id = id
        self.companyName = companyName
        self.address = address
        self.city = city
        self.state = state
        self.postal = postal
        self.country = country
        self.phone = phone
        self.email = email
        self.url = url
        self.checkedBy = checkedBy
        self.notes = notes
        self.status = status
        self.statusColor = statusColor
        self.identifier =  NSUUID()
        self.barnivoreChecked = barnivoreChecked
        self.alcoholType = alcoholType
    }
}

不幸的是我收到错误声明:

  

"自"在self.init call之前使用。

如果我尝试使用self.init(),我会收到错误:

  

无法调用' Alcohol.init'没有争论。

非常感谢任何帮助或建议。

1 个答案:

答案 0 :(得分:1)

在Swift 3中,在方便初始值设定项中尝试访问self之前,便捷初始化程序必须在同一个类中调用指定的初始值设定项。

我建议您将便利初始化程序更改为:

convenience init?(barnivoreDictionary: [String: AnyObject]) {
    guard
        let id = barnivoreDictionary[Constants.kID] as? Int,
        let companyName = barnivoreDictionary[Constants.kCompanyName] as? String,
        let address = barnivoreDictionary[Constants.kAddress] as? String,
        let city = barnivoreDictionary[Constants.kCity] as? String,
        let state = barnivoreDictionary[Constants.kState] as? String,
        let postal = barnivoreDictionary[Constants.kPostal] as? String,
        let country = barnivoreDictionary[Constants.kCountry] as? String,
        let phone = barnivoreDictionary[Constants.kPhone] as? String,
        let email = barnivoreDictionary[Constants.kEmail] as? String,
        let url = barnivoreDictionary[Constants.kURL] as? String,
        let checkedBy = barnivoreDictionary[Constants.kCheckedBy] as? String,
        let notes = barnivoreDictionary[Constants.kNotes] as? String,
        let status = barnivoreDictionary[Constants.kStatus] as? String,
        let statusColor = barnivoreDictionary[Constants.kStatusColor] as? String else {
            return nil
    }

    self.init(id: id, companyName: companyName, address: address, city: city, state: state, postal: postal, country: country, phone: phone, email: email, url: url, checkedBy: checkedBy, notes: notes, status: status, statusColor: statusColor, alcoholType: alcoholType)
}

请注意,您无需在self.init...内拨打guard