Can we use only NSString objects as key in an NSDictionary? How do we know which objec开发者_如何学Pythonts can be used and which cannot?
From the documentation:
In general, a key can be any object (provided that it conforms to the
NSCopying
protocol), but note that when using key-value coding the key must be a string (see “Key-Value Coding Fundamentals”).
So you can use anything copyable besides strings, but they'll be problematic with KVC. I just use strings for keys to keep things safe, consistent and simple.
You can use anything that conforms to NSCopying. That is, you can use id
- type objects, as long as they conform to NSCoding
protocol.
In instances where the key is NSString, then isEqualToString: is called for retrieval. Otherwise, isEqual: is called on the object to determine whether the key matches the requested key.
The key (and value for that matter) cannot be nil
or NULL
. They can, however, be [NSNull null]
.
精彩评论