(Key: Anyobject, Value: Anyobject)' Does Not Have a Member Named 'subscript'

(key: AnyObject, value: AnyObject)' does not have a member named 'subscript'

'(key: AnyObject, value: AnyObject)' indicates that item is not an Dictionary but is a Tuple with a single key/value pair.

Iterating dictionaries in swift interates through tuples:

for (key, value) in json {
println(key, value)
}

Your for loop indicates that you are probably wanting a json Array of tuples instead of a json Dictionary.

item["id"] would give you a compile time error if you declared the parameter as a tuple. It seems you stumbled onto something hidden with the language with how either tuples or subscripts work under the hood.

More on Subscripts

More on Types (Tuples)

Type '(String, AnyObject)' has no subscript members

The reason is this very curious phrase

for post in posts

The problem is that posts is a dictionary (not an array). So you are asking to cycle through a dictionary. That is a very strange thing to do. And the result when you do it is a little strange: each time through the cycle, you get a tuple representing one key-value pair.

Type '(String, AnyObject)' has no subscript members in swift

Most probably you want to filter arrProductCard array instead of productDict, which doesn't make sense. Try this:

let filteredProducts = arrProductCard.filter{
guard let groupId = $0["groupid"] as? String else { return false }
return groupId != "1"
}

You should avoid forced unwrapping whenever you can. Note that your code inside filter closure will crash if there is no groupid value in the dictionary or if it is not a string.

Edit:

If you're using NSMutableArray for some reason, you can just filter it with a predicate:

let mutableArray = NSMutableArray(array: [["groupid": "1"], ["groupid": "2"]])    
let groupIdPredicate = NSPredicate(format: "groupid != %@", "1")
mutableArray.filter(using: groupIdPredicate)

However, I would strongly recommend to use Swift native collections.

(String: AnyObject) does not have a member named 'subscript'

The error message tells you exactly what the problem is. Your dictionary values are typed as AnyObject. I know you know that this value is a string array, but Swift does not know that; it knows only what you told it, that this is an AnyObject. But AnyObject can't be subscripted (in fact, you can't do much with it at all). If you want to use subscripting, you need to tell Swift that this is not an AnyObject but rather an Array of some sort (here, an array of String).

There is then a second problem, which is that dict["participants"] is not in fact even an AnyObject - it is an Optional wrapping an AnyObject. So you will have to unwrap it and cast it in order to subscript it.

There is then a third problem, which is that you can't mutate an array value inside a dictionary in place. You will have to extract the value, mutate it, and then replace it.

So, your entire code will look like this:

var dict = [String:AnyObject]()
dict["participants"] = ["foo", "bar"]
var arr = dict["participants"] as [String] // unwrap the optional and cast
arr[0] = "baz" // now we can subscript!
dict["participants"] = arr // but now we have to write back into the dict

Extra for experts: If you want to be disgustingly cool and Swifty (and who doesn't??), you can perform the mutation and the assignment in one move by using a define-and-call anonymous function, like this:

var dict = [String:AnyObject]()
dict["participants"] = ["foo", "bar"]
dict["participants"] = {
var arr = dict["participants"] as [String]
arr[0] = "baz"
return arr
}()

Type '(key: String, value: Any)' has no subscript members

You are casting the array to a dictionary which cannot work.

The easiest solution is to (optional down)cast users to specific [[String : Any]]

if let users = object["usersList"] as? [[String : Any]] {
for user in users {
print(user["id"])
}
}

Cannot subscript a value of type 'DictionaryNSObject, AnyObject' with an index of type 'String error

Your code uses a lot of inappropriate objective-c-ish API.

This is the recommended Swift way to read and parse a property list file

if #available(iOS 7.1, *) {   
//ローカルplist格納先を取得
do {
let strWorkURL = try FileManager.default.url(for: .libraryDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let strPlistURL = strWorkURL.appendingPathComponent("\(S_DIRECTORY_NAME)/\(S_PLIST_NAME)")
let strPlistData = try Data(contentsOf: strPlistURL)
guard let dicPref = try PropertyListSerialization.propertyList(from: strPlistData, format: nil) as? [String:Any] else { return }
for (key, value) in dicPref {
UserDefaults.standard.set(value, forKey: key)
}
} catch { print(error) }
}

And don't use performSelector(inBackground:) either. Use GCD

DispatchQueue.global().async {
stopIndicator()
}

Swift: '(String) - AnyObject?' does not have a member named 'subscript'

You are declaring wrong type of userData. It must be [String: AnyObject?] because your values are optionals.

And don't forget to change this line "linkedInUrl" : pfUser.valueForKey["linkedInUrl"] as? String with this line "linkedInUrl" : pfUser.valueForKey("linkedInUrl") as? String.

And your code will be:

var userData : [String: AnyObject?] = [
"fullName" : pfUser.valueForKey("fullName") as? String,
"latitude" : pfUser.valueForKey("latitude") as? Double,
"longitude" : pfUser.valueForKey("longitude") as? Double,
"email" : pfUser.valueForKey("email") as? String,
"linkedInUrl" : pfUser.valueForKey("linkedInUrl") as? String
]


Related Topics



Leave a reply



Submit