Swift:'(Nsobject, Anyobject)' Does Not Have a Member Named 'Subscript'

[NSObject : AnyObject]?' does not have a member named 'subscript' in Xcode 6 Beta 6

The error message is saying you can't do [] on Optional value. What you need to do is unwrap it.

error.userInfo!["error"] as NSString

or if you want to be safe

if let errorString = error.userInfo?["error"] as NSString {
println(errorString)
}

[NSObject : AnyObject]?' does not have a member named 'subscript' error in Xcode 6 beta 6

As mentioned in the Xcode 6 beta 6 release notes, a large number of Foundation APIs have been audited for optional conformance.
These changes replace T! with either T? or T depending on whether the value can be null (or not) respectively.

notification.userInfo is now an optional dictionary:

class NSNotification : NSObject, NSCopying, NSCoding {
// ...
var userInfo: [NSObject : AnyObject]? { get }
// ...
}

so you have to unwrap it. If you know that userInfo is not nil then
you can simply use a "forced unwrapping":

var info = notification.userInfo!

but note that this will crash at runtime if userInfo is nil.

Otherwise better use an optional assignment:

if let info = notification.userInfo {
var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
} else {
// no userInfo dictionary present
}

AnyObject? does not have a member named 'subscript' swift

I tried this and it works.

if let config: AnyObject? = fields[i]["config"] {
if let options: AnyObject? = config?["options"] {
println("options : \(options)")
}
}

(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
}()

(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)

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
]

Type 'NSObject?' has no subscript members

The declaration of mainDict in the function signature has to be [String :[String:Any]]. Or you can declare it as [String:Any] and then, you need to cast the sub as [String:Any]

So the function should be

func makeItPossible(mainDict : [String:Any]){
if let sub= mainDict["keys"] as [String:Any], let keyThree = sub["three"]{
print(keyThree)
}

Updated to use conditional binding.



Related Topics



Leave a reply



Submit