[ad_1]
I am studying Swift and a few iOS growth. Right here is my code which I am making an attempt to determine why XCode is complaining.
non-public func rollMyDice() {
let diceRollImages = [
UIImage(named: "DiceOne"),
UIImage(named: "DiceTwo"),
UIImage(named: "DiceThree"),
UIImage(named: "DiceFour"),
UIImage(named: "DiceFive"),
UIImage(named: "DiceSix")
]
diceOneImageView.picture = diceRollImages.randomElement()
diceTwoImageView.picture = diceRollImages.randomElement()
}
So on this case, diceOneImageView.picture = diceRollImages.randomElement() will complain that it can not assign UIImage?? to UIImage?.
ImageView.picture is of kind UIImage?. What I do not perceive is why is randomElement() right here is returning UIImage??.
What does UIImage?? imply? I assumed ?? was the nil coalescsing operator, so I am undecided why it is a part of some return kind.
Additionally studying the documentation on randomElement(), it ought to return ?. So on this case, I’d anticipate diceRollImages.randomElement() to return UIImage? which ought to swimsuit diceOneImageView.picture.
What is going on right here? I do know I can repair it through the use of ! or utilizing nil coalescing and so on. to make it work. Simply do not get what is going on on.
[ad_2]
