cocoa objective-c NSDictionary with integer objects
Why does dict2
show a warning about its object not being a pointer when dict开发者_开发百科1
shows no such warning?
Shouldn't both show this warning? both are integers..
erg.
NSDictionary *dict1 = [NSDictionary dictionaryWithObject:0 forKey:TAG_KEY];
NSDictionary *dict2 = [NSDictionary dictionaryWithObject:1 forKey:TAG_KEY];
Obviously, ints are not objects — but 0 in a pointer context is interpreted as nil/NULL. In fact, that's precisely how nil is defined. From objc.h:
#define Nil 0 /* id of Nil class */
#define nil 0 /* id of Nil instance */
You can use class NSNumber
if you want integers in the dictionary.
精彩评论