Manish Pandey
Manish Pandey

Reputation: 23

Cannot assign a value of type 'UIImage?' to type 'String!'

I am trying to call images but getting error "Cannot assign value of type 'UIImage?' to type 'String!'" in the line "vc.sentData2 = image".

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "DetailView"){
        let vc = segue.destinationViewController as! DetailViewController

        if let indexPath = self.tableView.indexPathForSelectedRow{
            let Title = names[indexPath.row] as String
            vc.sentData1 = Title

            let url = NSURL(string: images[indexPath.row])
            let data = NSData(contentsOfURL: url!)
            let image = UIImage(data: data!)
            vc.sentData2 = image
        }
    }
}

I would be thankful for help.

Upvotes: 0

Views: 6717

Answers (1)

David Berry
David Berry

Reputation: 41226

Fairly obviously, in your definition of DetailViewController you've declared sentData2 as a String! And, in this case you're trying to assign a UIImage? (the result of UIImage(data:) is always a UIImage? since there may be no valid image) to a String!.

So, change

var sentData2: String!

in DetailViewController to

var sentData2: UIImage?

Upvotes: 2

Related Questions