Monday, August 15, 2022
HomeiOS Developmentios - Worth of non-obligatory sort 'UIImage?' have to be unwrapped to...

ios – Worth of non-obligatory sort ‘UIImage?’ have to be unwrapped to a worth of sort ‘UIImage’

[ad_1]

The Swift compiler takes sort security very severely, so if a return is not assured, will probably be returned as an non-obligatory. On this case, the compiler does not learn about “ferrari”, or you possibly can use this perform later with “lamborghini” and overlook so as to add it to your property, otherwise you name it “Lamborghini”. Both method, your app will crash.

Your higher choice can be to deal with the non-obligatory UIImage within the func itself, like:

func makePost(picture: UIImage?,appUser: Consumer) {

    // The guard assertion safely unwraps your non-obligatory and makes certain it
    // is just not nil earlier than utilizing it.
    guard let picture = picture else { return }

    // Now it's unwrapped and protected to make use of.
    let publish: Put up = Put up(ersteller: appUser, picture: picture, datum: "2022")
    
    feed.append(publish)
    
    for i in 0..<associates.rely {
        associates[i].feed.append(publish)
    }
}

If ‘picture’ is nil and never a UIImage, then nothing will occur, however your app will not crash.

[ad_2]

RELATED ARTICLES

Most Popular

Recent Comments