vendredi 24 avril 2020

Swift handling nil

I am new to Swift and this is the code that gives me an error and somehow it gets past "lastName != nil" even though it has no text.

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

func imageFromInitials(firstName: String?, lastName: String?, withBlock: @escaping (_ image: UIImage) -> Void) {

var string: String!
var size = 36

if firstName != nil && lastName != nil {
    string = String(firstName!.first!).uppercased() + String(lastName!.first!).uppercased()
} else {
    string = String(firstName!.first!).uppercased()
    size = 72
}

I am confused and not sure why validation did not work

Aucun commentaire:

Enregistrer un commentaire