Failable Initializers
Failable Initializers, başarısız olabilen init metotlarıdır. Init metot başarısız olduğunda nesne yönelimli programlama dillerinde exception fırlatmaktadır. Swift programala dilinde ise Failable Initializers ile döndürmek istediğimiz değeri tanımlayabiliriz. Örneğin kişi id'si 9 karakterden farklı olursa boş döndürülmesi gibi.
// failable initializers
struct Person {
var id: String
init?(id: String) {
if id.count == 9 {
self.id = id
} else {
return nil
}
}
}
Referanslar:
Failable initializers - a free Hacking with Swift tutorial
Learn Swift coding for iOS with these free tutorials

Failable Initializers - Swift Blog
Get the latest news and helpful tips on the Swift programming language from the engineers who created it.
