Convenience initializer with optional property

My object has an integer identifier. Since this is a required property, I do not define it as optional and require it in the specified initializer:

class Thing {

    var uniqueID: Int
    var name: String?

    init (uniqueID: Int) {
        self.uniqueID = uniqueID
    }       

}

Since I am creating one of them from some JSON, use is according to:

if let uniqueID = dictionary["id"] as? Int {
    let thing = Thing(uniqueID: unique)
}

Next, I would like to be able to add a convenient initializer to the Thing class, which takes a dictionary object and sets properties accordingly. This includes mandatory uniqueIDand some other additional properties. My best efforts so far:

convenience init (dictionary: [String: AnyObject]) {
    if let uniqueID = dictionary["id"] as? Int {
        self.init(uniqueID: uniqueID)
        //set other values here?
    }
        //or here?
}

But, of course, this is not enough, since the specified initializer is not called on all paths of the conditional expression.

? ? , uniqueID ?

+4
2

. - failable initialisers:

convenience init?(dictionary: [String: AnyObject]) {
    if let uniqueID = dictionary["id"] as? Int {
        self.init(uniqueID: uniqueID)
    } else {
        self.init(uniqueID: -1)
        return nil
    }
}

( , / ), :

class func fromDictionary(dictionary: [String: AnyObject]) -> Thing? {
    if let uniqueID = dictionary["id"] as? Int {
        return self.init(uniqueID: uniqueID)
    }

    return nil
}

, :

class Thing {
    var uniqueID: Int
    var name: String?

    init(uniqueID: Int) {
        self.uniqueID = uniqueID
    }

    convenience init?(dictionary: [String: AnyObject]) {
        if let uniqueID = dictionary["id"] as? Int {
            self.init(uniqueID: uniqueID)
        } else {
            self.init(uniqueID: -1)
            return nil
        }
    }

    class func fromDictionary(dictionary: [String: AnyObject]) -> Thing? {
        if let uniqueID = dictionary["id"] as? Int {
            return self.init(uniqueID: uniqueID)
        }

        return nil
    }
}

let firstThing = Thing(uniqueID: 1)
let secondThing = Thing(dictionary: ["id": 2])
let thirdThing = Thing(dictionary: ["not_id": 3])
let forthThing = Thing.fromDictionary(["id": 4])
let fithThing = Thing.fromDictionary(["not_id": 4])
+5

, , , , nil.

Swift , , - .

:

convenience init?(dictionary: [String: AnyObject]) {
    if let uniqueID = dictionary["id"] as? Int {
        self.init(uniqueID: uniqueID)
    } else {
        self.init(uniqueID: 0)
        return nil
    }
}

, , , , .

, :

init(uniqueID: Int, name: String? = nil) {
    self.uniqueID = uniqueID
    self.name = name
}

:

let thing1 = Thing(1)
let thing2 = Thing(2, nil)
let thing3 = Thing(3, "foo")
let thing4 = Thing(4, myUnwrappedStringVar)
let thing5 = Thing(5, myWrappedStringOptional)

.

, , Int.

convenience init?(uniqueID: Int? = nil, name: String? = nil) {
    if let id = uniqueID {
        self.init(uniqueID: id, name: name)
    } else {
        self.init(uniqueID: 0)
        return nil
    }
}

Int? uniqueID , nil.

, , .

convenience init?(dictionary: [String: AnyObject]) {
    let uniqueID = dictionary["id"] as? Int
    let name = dictionary["name"] as? String
    self.init(uniqueID: uniqueID, name: name)
}

, nil , , , .

, , id, , Int, let uniqueID nil, , , , Int?, nil, nil, , , , , nil.

+4

All Articles