Swift Optional Type not Unwrapped

Kareem Arab

I've been having a problem probably due to the new updated version of Swift. The problem is that this line of code keeps producing an error saying that:

"value of optional type '()?' not unwrapped; did you mean to use '!' or '?'?

cell?.hypeImageView?.image = UIImage(data: imageData)

This is the entire function:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {

    var cell:HypeTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? HypeTableViewCell
    if(cell == nil) {
        cell = NSBundle.mainBundle().loadNibNamed("HypeTableViewCell", owner: self, options: nil)[0] as? HypeTableViewCell
    }

    if let pfObject = object {
        cell?.hypeNameLabel?.text = pfObject["name"] as? String

        var votes:Int? = pfObject["votes"] as? Int
        if votes == nil {
            votes = 0
        }
        cell?.hypeVotesLabel?.text = "\(votes!) votes"

        let credit:String? = pfObject["cc_by"] as? String //if prob change to var
        if credit != nil {
            cell?.hypeCreditLabel?.text = "\(credit!) / CC 2.0"
        }

        cell?.hypeImageView?.image = nil
        if var urlString:String? = pfObject["url"] as? String {
            var url:NSURL? = NSURL(string: urlString!)
            if var url:NSURL? = NSURL(string: urlString!) {
                var error:NSError?
                var request:NSURLRequest = NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, timeoutInterval: 5.0)

                NSOperationQueue.mainQueue().cancelAllOperations()

                NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {
                    (response:NSURLResponse!, imageData:NSData!, error:NSError!) -> Void in

                    cell?.hypeImageView?.image = UIImage(data: imageData)

                })
            }
        }

    }

    return cell

}

How can I fix this issue?

Steve Wilford

Assuming you're using Swift 2.0 change the signature for the completionHandler to expect optionals rather than implicitly unwrapped optionals, then the problem will go away.

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response:NSURLResponse?, data:NSData?, error:NSError?) -> Void in

    guard let imageData = data else {
        assertionFailure("No data found")
        return
    }

    cell?.hypeImageView?.image = UIImage(data: imageData)
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Value of optional type CGFloat not unwrapped error in Swift

From Dev

Swift 2: !, ? -" Value of optional type "..." not unwrapped"

From Dev

Optional Type 'String??' Not Unwrapped

From Dev

How to get the unwrapped type from an optional type in Swift?

From Dev

Swift Optional Unwrapped still crashes

From Dev

Value of optional type String? not unwrapped

From Dev

Swift 3 | JSON Value of optional type '[String : Any] not unwrapped, did mean to use '!' or '?'?

From Dev

Issue with Swift: value of optional type '[Int : Int]?' not unwrapped; did you mean to use '!' or '?'?

From Dev

Value of optional type 'Date?' not unwrapped; did you mean to use '!' or '?'? In Swift 4 XCode 9

From Dev

Swift unary operator with implicitly unwrapped optional

From Dev

Why is a Swift implicitly unwrapped optional `nil`?

From Dev

Swift - Check nil of an unwrapped optional variable

From Dev

Swift unary operator with implicitly unwrapped optional

From Dev

Swift 3: Meaning of parenthesis around unwrapped optional

From Dev

Implicitly unwrapped optional from init!() in Swift 3.1

From Dev

Why the default type of the label is a forced unwrapped optional?

From Dev

Why the default type of the label is a forced unwrapped optional?

From Dev

Optional Type Dictionary yields "Value of Optional type not unwrapped"

From Dev

Swift Type vs explicitly unwrapped Type

From Dev

Swift's use of Implicitly Unwrapped Optional before availability of nullability

From Dev

Value of optional type 'Int?' not unwrapped; did you mean to use '!' or '?'?

From Dev

Returning Tuple From Delegate (Value of optional type not unwrapped)

From Dev

Value of optional Type "AnyObject?" not unwrapped did you mean to use ! or?

From Dev

Value of optional type 'String?' not unwrapped; did you mean to use '!' or '?'?

From Dev

Returning Tuple From Delegate (Value of optional type not unwrapped)

From Dev

Value of optional type 'NSNumber' not unwrapped; did you mean to use '!' or '?'?

From Dev

Value of optional type 'UIImage?' not unwrapped; did you mean to use '!' or '?'? Insert '!'

From Dev

Incrementing an implicitly unwrapped optional

From Dev

Returning an implicitly unwrapped optional

Related Related

  1. 1

    Value of optional type CGFloat not unwrapped error in Swift

  2. 2

    Swift 2: !, ? -" Value of optional type "..." not unwrapped"

  3. 3

    Optional Type 'String??' Not Unwrapped

  4. 4

    How to get the unwrapped type from an optional type in Swift?

  5. 5

    Swift Optional Unwrapped still crashes

  6. 6

    Value of optional type String? not unwrapped

  7. 7

    Swift 3 | JSON Value of optional type '[String : Any] not unwrapped, did mean to use '!' or '?'?

  8. 8

    Issue with Swift: value of optional type '[Int : Int]?' not unwrapped; did you mean to use '!' or '?'?

  9. 9

    Value of optional type 'Date?' not unwrapped; did you mean to use '!' or '?'? In Swift 4 XCode 9

  10. 10

    Swift unary operator with implicitly unwrapped optional

  11. 11

    Why is a Swift implicitly unwrapped optional `nil`?

  12. 12

    Swift - Check nil of an unwrapped optional variable

  13. 13

    Swift unary operator with implicitly unwrapped optional

  14. 14

    Swift 3: Meaning of parenthesis around unwrapped optional

  15. 15

    Implicitly unwrapped optional from init!() in Swift 3.1

  16. 16

    Why the default type of the label is a forced unwrapped optional?

  17. 17

    Why the default type of the label is a forced unwrapped optional?

  18. 18

    Optional Type Dictionary yields "Value of Optional type not unwrapped"

  19. 19

    Swift Type vs explicitly unwrapped Type

  20. 20

    Swift's use of Implicitly Unwrapped Optional before availability of nullability

  21. 21

    Value of optional type 'Int?' not unwrapped; did you mean to use '!' or '?'?

  22. 22

    Returning Tuple From Delegate (Value of optional type not unwrapped)

  23. 23

    Value of optional Type "AnyObject?" not unwrapped did you mean to use ! or?

  24. 24

    Value of optional type 'String?' not unwrapped; did you mean to use '!' or '?'?

  25. 25

    Returning Tuple From Delegate (Value of optional type not unwrapped)

  26. 26

    Value of optional type 'NSNumber' not unwrapped; did you mean to use '!' or '?'?

  27. 27

    Value of optional type 'UIImage?' not unwrapped; did you mean to use '!' or '?'? Insert '!'

  28. 28

    Incrementing an implicitly unwrapped optional

  29. 29

    Returning an implicitly unwrapped optional

HotTag

Archive