Swift forcing me to unwrap HELP

code :
for x in 0 …< data1.count {
let cool = data1[x].photoUrl?[0] as AnyObject?
let convertString = cool as! NSString
let stringData = convertString.data(using: String.Encoding.utf8.rawValue)
let imageData = UIImage(data:stringData)
print(imageData as Any)
photoArray.append(imageData as AnyObject)

What I’m trying to do:
data1.photoUrl = my array that consists of string urls that are images. I am looping through each string and put them into an object called cool. I’m converting that object into a NSString.
From there I’m converting that string in data and i want to convert that data to a uiimage.
swift is giving me this error. “Value of optional type ‘Data?’ not unwrapped; did you mean to use ‘!’ or ‘?’?” but when i do that it produces nil in my console log. how can i use ? instead of ! and also how can i convert my data to uiimage so i can put it into my table cell image view.

Hi @ghost6ix,
you know the reason why Swift forces you to unwrap?

For any function that could not return a value, Swift uses optionals. So you have the result as an optional and the other function requires a non optional value, that is when you have to unwrap the values.

the way to manage that is not simply by adding a ! to unwrap but use the guard or if let to handle a case where there is no value to unwrap. The code would have also changed with the newer versions of Swift.