jonasdickel
jonasdickel

Reputation: 311

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

    }
}

Upvotes: 31

Views: 39921

Answers (5)

sv_lane
sv_lane

Reputation: 3089

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.

Upvotes: 1

Scott Fister
Scott Fister

Reputation: 1283

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

if let foo = dictionary["bar"] as? [String]

Upvotes: 22

Shaked Sayag
Shaked Sayag

Reputation: 5792

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

Upvotes: 24

nstein
nstein

Reputation: 349

As Eric and Eugene mentioned in their comments it is impossible to review the issue you are having without knowing the selectedRestaurant type. That is after all why you are getting the compiler ambiguity error.

I have to respectfully disagree with MikeG though. The problem is not one of a valid subscript. You'd be getting that kind of error, if for example you had a selectedRestaurant type of [NSNumber:AnyObject], where clearly String is no longer valid since the dictionary key could only be an NSNumber.

Upvotes: 0

MikeG
MikeG

Reputation: 4044

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

Upvotes: 2

Related Questions