Swift Error: Ambiguous reference to member 'subscript'
Asked Answered
B

5

31

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

    }
}
Brandnew answered 25/1, 2016 at 14:37 Comment(2)
Show us how selectedRestaurant is declared and what type it is.Ungley
How selectedRestaurant is declared? From the code snipped you've posted we don't know the type and / or data that you stores in that var. Also, the rest of your code, where you load an image from URL, does not have any subscript.Fleam
J
24

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
Jamesy answered 19/10, 2016 at 12:50 Comment(0)
U
22

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

if let foo = dictionary["bar"] as? [String]
Unpolled answered 1/8, 2016 at 23:59 Comment(1)
This is what I needed as well. Thanks!Favors
T
2

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

Twitter answered 25/1, 2016 at 14:52 Comment(0)
P
1

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.

Predicate answered 14/3, 2017 at 18:33 Comment(0)
U
0

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.

Uvarovite answered 20/6, 2016 at 9:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.