Ambiguous Use of Subscript in Swift
The problem is that you are using NSArray:
myQuestionsArray = NSArray(contentsOfFile: path)
This means that myQuestionArray
is an NSArray. But an NSArray has no type information about its elements. Thus, when you get to this line:
let currentQuestionDict = myQuestionsArray!.objectAtIndex(count)
...Swift has no type information, and has to make currentQuestionDict
an AnyObject. But you can't subscript an AnyObject, so expressions like currentQuestionDict["choice1"]
cannot compile.
The solution is to use Swift types. If you know what currentQuestionDict
really is, type it as that type. At the very least, since you seem to believe it is a dictionary, make it one; type it as [NSObject:AnyObject]
(and more specific if possible). You can do this in several ways; one way is by casting when you create the variable:
let currentQuestionDict =
myQuestionsArray!.objectAtIndex(count) as! [NSObject:AnyObject]
In short, never use NSArray and NSDictionary if you can avoid it (and you can usually avoid it). If you receive one from Objective-C, type it as what it really is, so that Swift can work with it.
Ambiguous use of 'subscript' Swift 3 compile error
First of all the JSON dictionary type in Swift 3 is [String:Any]
.
The ambiguous use
reason is that the compiler doesn't know the type of allContacts["data"]
. It's obviously an array but you need to tell the compiler. And please don't use the ugly C-style index based for loops in Swift at all. If you need index
and object
in a repeat loop use enumerated()
.
if let arrJSON = allContacts["data"] as? [[String : Any]] {
for aObject in arrJSON {
if ChooseSubject.mineFagKoder.contains(aObject["subject"] as! String) { ...
Ambiguous Use of Subscript in Swift?
let newObject = arr[indexPath.section] as? [AnyObject]
and then use your code as
let newProductDic = newObject[indexPath.row]
Ambiguous use of 'subscript(_:)'
A JSON object is never unspecified AnyObject
. If you expect a dictionary cast it to dictionary
let myJson = try JSONSerialization.jsonObject(with: content) as! [String:Any]
This fixes the error because the compiler now knows the real type.
And never specify .mutableContainers
. The option has no effect in Swift
Ambiguous Use of Subscript (Swift 3)
My preferred way of dealing with data is to unwrap the FIRDataSnapshot
as late as possible.
ref!.observe(.value, with: { (snapshot) in
for child in snapshot.children {
let msg = child as! FIRDataSnapshot
print("\(msg.key): \(msg.value!)")
let val = msg.value! as! [String:Any]
print("\(val["name"]!): \(val["message"]!)")
}
})
Swift 3 Ambiguous Use of Subscript
You need to specify the type of your variable alert
. The problem is that it cannot apply subscript to type AnyObject
Subscripts in Swift 3
Classes, structures, and enumerations can define subscripts, which are shortcuts for accessing the member elements of a collection, list, or sequence. You use subscripts to set and retrieve values by index without needing separate methods for setting and retrieval. For example, you access elements in an Array instance as someArray[index] and elements in a Dictionary instance as someDictionary[key].
Error message = Ambiguous use of 'subscript(_:)' Found this candidate Problem of casting Array of Dictionary
You've defined models
as [AnyObject]
i.e. an array of AnyObject
. So model
is an instance of AnyObject
and the compiler wouldn't be able to infer what it is and thus it can't be subscripted without a more specific type.
Should work if you use a new variable with type [[String: Any]]
.
[Dictionary<String,Any>]
is equivalent, but to me the shorter syntax is more readable.
Ambiguous use of 'subscript' in swift 3 using xcode 8
if let tracks = readableJSON["tracks"] as? JSONStandard
{
if let items = tracks["items"] as? NSArray
{
for i in 0..<items.count
{
let item = items[i] as? JSONStandard
let name = item?["name"] as! String
names.append(name)
self.tableView.reloadData()
}
}
}
I got my answer with line if let items = tracks["items"] as? NSArray
instead of if let items = tracks["items"]
Here Simulator takes types itself and for device we have to specify data type as i have writtern as? NSArray
for items
Related Topics
Grab Frames from Video Using Swift
"Unrecognized Selector Sent to Instance" in Swift
In Swift, Can You Split a String by Another String, Not Just a Character
Swift, Auto Resize Custom Table View Cells
Sprite Kit Physicsbody.Resting Behavior
Swift: How to Delete Part of Audio
Expandable Sections Uitableview Indexpath Swift
How to Use Keywords as Parameter Names in Swift
Custom Swift Encoder/Decoder for the Strings Resource Format
Nstextfield, Change Text in Swift
Bold Part of String in Uitextview Swift
Swift Convert String to Unsafemutablepointer<Int8>
Swift/Uiview/Drawrect - How to Get Drawrect to Update When Required
Keyboard Overlaying Action Sheet in iOS 13.1 on Cncontactviewcontroller
Turn Off Xcode's Unused Variable Warnings While Typing