Jimmery
Jimmery

Reputation: 10119

Cannot convert the expression's type '(@!value UIImage?) -> $T3' to type 'UIImage?'

This code:

var uiiv = UIImageView()
let nsurl = NSURL(string: "http://...")
var err: NSError?
let nsd: NSData = NSData(contentsOfURL: nsurl, options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &err)!
var img = UIImage(data: nsd)
uiiv.image(img)

Produces this error:

Cannot convert the expression's type '(@!value UIImage?) -> $T3' to type 'UIImage?'

Can anybody explain to me how to fix this error, and also what the error actually means, it would be helpful to me in the future to understand what things like $T3 and @!value means.

Upvotes: 0

Views: 1127

Answers (2)

Antonio
Antonio

Reputation: 72760

The NSData initializer you are using expects a non optional NSURL, but this initializer NSURL(string: "http://...") returns an optional. You can either use optional binding or forced unwrapping - in the latter case:

let nsurl = NSURL(string: "http://...")!

or

let nsd: NSData = NSData(contentsOfURL: nsurl!, options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &err)!

Moreover, the NSData initializer returns an optional itself, and I see you've used forced unwrapping - in this case I would use optional binding, because nsd can be nil:

var uiiv = UIImageView()
let nsurl = NSURL(string: "http://...")!

var err: NSError?
let nsd = NSData(contentsOfURL: nsurl, options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &err)

if let nsd = nsd {
    var img = UIImage(data: nsd)
    uiiv.image = img
}

As for the error message:

Cannot convert the expression's type '(@!value UIImage?) -> $T3' to type 'UIImage?'

that's because you are using a property as if it were a method.

image is of UIImage? type, but you are using it as a function taking an UIImage? and returning an unspecified type T3.

By fixing the code properly, the error of course disappears:

uiiv.image = img

Upvotes: 1

rintaro
rintaro

Reputation: 51911

image in UIImageView is a property, not method.

you can just assign to it.

uiiv.image = img

Instead of uiiv.image(img)

Upvotes: 1

Related Questions