Skip to content

Instantly share code, notes, and snippets.

@darrarski
Created November 19, 2015 15:59
Show Gist options
  • Save darrarski/b8b8fd025556cbe5442f to your computer and use it in GitHub Desktop.
Save darrarski/b8b8fd025556cbe5442f to your computer and use it in GitHub Desktop.
Swift Failable Initializer
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)
}
}
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