[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
}
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.
event.touchesForView().AnyObject() not working in Xcode 6.3
In 1.2, touchesForView
now returns a native Swift Set
rather than an NSSet
, and Set
doesn't have an anyObject()
method.
It does have a first
method, which is much the same thing. Note, also, that you won't be able to use as?
any more, you'll have to cast it using as?
and handle the nil possibility, here's one approach:
func doSomethingOnDrag(sender: UIButton, event: UIEvent) {
if let touch = event.touchesForView(sender)?.first as? UITouch,
location = touch.locationInView(sender) {
// use location
}
}
AnyObject does not have a member named sort
Don't access the array as an "AnyObject". Try pulling it as an Array object.
var savedDataArray: Array<String>? = NSUserDefaults.standardUserDefaults().objectForKey("savedDataArray") as? Array<String>;
Substitute the "String" type for whatever you're pulling the objects in your Array as. This allows you to use the "sort" member.
(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)
Related Topics
How to Create a PDF File Programmatically in an iOS Application
How to Start an Ibeacon Transmitter in the Background
Does iPhone Support Hardware-Accelerated Aes Encryption
How to Add Custom Text in Nsdateformatter's Format String
How to Resize Views When Keyboard Pops Up Using Auto Layout
Not Getting the Email Using Google Authentication in Firebase
How to Implement Tableview Inside Tableview Cell in Swift 3
Constraints for Centering One Button When the Other Is Hidden
Swift: Change Status Bar Color for iOS 13
Programmatically Access Image Assets
How to Mute/Unmute Audio When Playing Video Using Mpmovieplayercontroller
Errors Building Xcode Project After Adding in Run Script Fatal Error: Lipo: Input File
Core Data Predicate:Unimplemented SQL Generation for Predicate
Swift String Escaping When Serializing to JSON Using Codable
Facebook App Requests Aren't Shown on iOS Devices
Hit Detection When Drawing Lines in iOS
Xcode Doesn't Write Anything to Output Console
iOS - Linking Framework Storyboard to Viewcontroller for Use in Main Project