Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Error: Ambiguous reference to member 'subscript'

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

    }
}
like image 582
jonasdickel Avatar asked Jan 25 '16 14:01

jonasdickel


4 Answers

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
like image 171
Shaked Sayag Avatar answered Nov 12 '22 02:11

Shaked Sayag


For me the answer was to specifically state the type of array I was casting to:

if let foo = dictionary["bar"] as? [String]
like image 38
Scott Fister Avatar answered Nov 12 '22 03:11

Scott Fister


It means that "Pictures" is not a valid subscript. It looks like you are creating a constant named pictures and you are trying to assign it a value of selectedRestaraunt["Pictures"] and then trying to cast it as an NSArray. If selectedrestaraunt is already an array, then what goes in the [] brackets after selectedRestaraunt should be an integer value which will refer to an index in the selectedRestaraunt array. Obviosuly "Pictures" is not an integer, it is a string.

If you are trying to access an array within an array. Meaning that Pictures is an array stored within the selectedRestarauntarray then you can access it by using selectedRestaraunt[index of Pictures array] where [index of pictures array] is an integer which is equal to the index number in which the Picutres array resides within the selectedRestaraunt array

like image 2
MikeG Avatar answered Nov 12 '22 04:11

MikeG


I managed to get this error in a somewhat weird way. I had code like this:

cell.textLabel = anArrayOfStrings[indexPath.item].uppercased()

And I was stumped as to why it couldn't figure out that this was an array, even though I very clearly declared its type. I broke the line in two and finally got a helpful error message:

let name = anArrayOfStrings[indexPath.item].uppercased()
cell.textLabel = name

I was trying to assign a String to a UILabel, but somehow the point at which the type inference engine failed was at the subscript.

So my advice to anyone stumped by this is to try to break up your statement into bite-sized chunks that the Swift type inference engine can more easily digest.

like image 2
bugloaf Avatar answered Nov 12 '22 02:11

bugloaf