Swift Error: Ambiguous reference to member 'subscript'

jonasdickel picture jonasdickel · Jan 25, 2016 · Viewed 39.7k times · Source

I'm new to coding and picked up some open source project to get the idea.

I'm getting the error:

Ambiguous reference to member 'subscript'

in the code below:

let pictures = ( selectedRestaurant["Pictures"] as! NSArray ) // Error

let picture = ( pictures[zoomedPhotoIndex] as! NSDictionary )

let pictureURL = picture["url"] as! String

let imageURL = NSURL(string: pictureURL)
let urlRequest = NSURLRequest(URL: imageURL!)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue()) {
    response, data, error in
    if error == nil && data != nil {

        self.imageView.image = UIImage(data: data!)
        self.imageView.contentMode = UIViewContentMode.ScaleAspectFit

    }
}

Answer

Shaked Sayag picture Shaked Sayag · Oct 19, 2016

Just specify explicitly what is the type of pictures:

So instead of:

let pictures = selectedRestaurant["Pictures"] as! NSArray

Write:

let pictures: NSArray = selectedRestaurant["Pictures"] as! NSArray