简体   繁体   中英

Why isn't a implicit optional required in a class's init method?

I'm following a tutorial from Ray Wenderlich's website about ARC in Swift and I'm curious to know why is a optional allowed but not an implicit optional when creating a class in a playground?

The playground code I have so far is:

class User {
    var name: String
    init(name: String) {
        self.name = name
        print("User \(name) is initialized")
    }
    deinit {
        print("User \(name) is being deallocated")
    }
}

class Phone {
    let model: String
    var owner: User?
    init(model: String) {
        self.model = model
        print("Phone \(model) is initialized")
    }
    deinit {
        print("Phone \(model) is being deallocated")
    }
}

do {
    let user1 = User(name: "John")
}
let user2 = User.init(name: "Berry")

In the Phone class, if I change the owner variable to an implicit optional with an exclamation point then the playground doesn't throw an error however if I remove the question mark or don't make it optional then I receive an error.

Doesn't an implicit optional force an app to crash if it isn't set?

Any help is appreciated to get a firm understanding of why an implicit optional is okay.

An implicit optional is still an optional, so it can be nil and it does not need to be set at the time of initialisation. You will, however, get still get an exception if you access it unconditionally when it is nil .

If your Phone class were:

class Phone {
    let model: String
    var owner: User!
    init(model: String) {
        self.model = model
        print("Phone \(model) is initialized")
    }
    deinit {
        print("Phone \(model) is being deallocated")
    }
}

and you said

var aPhone = Phone(model:"iPhone7")
let ownerName = aPhone.owner.name

Then you would get an exception on the second line because the implicitly unwrapped optional is nil . That line is the equivalent of writing let ownerName = aPhone.owner!.name if owner were of type User? .

Implicitly unwrapped optionals are very useful where a value cannot be assigned at initialisation, but a value will be assigned shortly thereafter, as it avoids the need to continually unwrap the variable.

For example, implicitly unwrapped optionals are often used for @IBOutlet properties in a view controller. The property will be set by the storyboard load process, but after the view controller object is initialised. Since you know that the property will have a value by the time any of your code runs, the use of an implicitly unwrapped optional is safe and you avoid having to continually unwrap the property.

Your sample Phone class is not a good use of implicitly unwrapped optionals; either a simple optional (as you have shown) or a required initialiser argument is appropriate depending on whether phones can be unowned or not.

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