# Swift forcing me to unwrap HELP

**URL:** <https://forums.kodeco.com/t/swift-forcing-me-to-unwrap-help/29880>\
**Category:** Swift\
**Created:** [May 19, 2017, 6:11am UTC](https://forums.kodeco.com/t/swift-forcing-me-to-unwrap-help/29880 "2017-05-19T06:11:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghost6ix](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@ghost6ix](https://forums.kodeco.com/u/ghost6ix)\
**Post date:** [May 19, 2017, 6:11am UTC](https://forums.kodeco.com/t/swift-forcing-me-to-unwrap-help/29880/1 "2017-05-19T06:11:34Z")

</div>

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)  
}  
print(photoArray)

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.

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [May 6, 2018, 1:13am UTC](https://forums.kodeco.com/t/swift-forcing-me-to-unwrap-help/29880/2 "2018-05-06T01:13:47Z")

</div>

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.

cheers,

Jayant
