Created
November 19, 2015 15:59
-
-
Save darrarski/b8b8fd025556cbe5442f to your computer and use it in GitHub Desktop.
Swift Failable Initializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class User { | |
let name: String | |
init(name: String) { | |
self.name = name | |
} | |
convenience init?(name: String?) { | |
guard let name = name else { return nil } | |
self.init(name: name) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class User { | |
let name: String | |
init?(name: String?) { | |
guard let name = name else { return nil } // Error: "All stored properties of a class instance must be initialized before returning nil from an initializer" | |
self.name = name | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment