[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
}
[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)
}
Swift : '(NSObject, AnyObject)' does not have a member named 'subscript'
The shortest one is:
// Xcode 6.0.1
func handleRemoteNotifiation(userInfo: [NSObject : AnyObject]) {
if let badge = [userInfo["aps"]?["badge"]][0] as? Int {
self.updateAppIconBadgeNumber(badge)
}
}
// Xcode 6.1
func handleRemoteNotifiation(userInfo: [NSObject : AnyObject]) {
if let badge = userInfo["aps"]?["badge"] as? Int {
self.updateAppIconBadgeNumber(badge)
}
}
?
between ["aps"]
and ["badge"]
is called "Optional Chaining".
You need this because userInfo["aps"]
can returns nil
.
And you don't have to cast it to [String : AnyObject]
because every AnyObject
has 'subscript' member.
And, Why we need [ ... ][0]
in Xcode 6.0.1 is... I don't know :( .a bug, maybe.
[AnyObject]? does not have a member named 'mutableCopy'
The fetchedObjects
property of NSFetchedResultsController
is defined as [AnyObject]?
- so you don't have to convert it to NSArray
or NSMutableArray
- just use it as is.
Since an array in swift is a value type, it is always copied by value and not by reference, which means a copy is created by just assigning to a variable. So in:
var sortingArray = fetchedResultsController?.fetchedObjects
a copy of fetchedObjects
will be copied into sortingArray
.
(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)
(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
}()
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)")
}
}
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
How to Style Uitextview to Like Rounded Rect Text Field
Uinavigationcontroller "Back Button" Custom Text
Implement Pushkit and Test in Development Behavior
Firebase Cloud Messaging - Check Existing or Available Topics
How to Rotate Orientation Programmatically in Swift
Can't Assign Multiple Buttons to Uinavigationitem When Using Storyboard with iOS 5
Uiimage Imagenamed Returns Nil
Xcode Canvas for Swiftui Previews Does Not Show Up
Custom Rounding Corners on Uiview
How to Set iOS Status Bar Background Color in React Native
How Big Should a Uibarbuttonitem Image Be
iPhone - Draw Transparent Rectangle on Uiview to Reveal View Beneath
Posting JSON Data Using Afnetworking 2.0
How to Embed Youtube Video on iOS and Play It Directly on Uiwebview Without Full Screen