当前位置: 动力学知识库 > 问答 > 编程问答 >

cocoa - Content of a NSMutableDictionnary erratically returned

问题描述:

I am trying to add keys in a dictionnary in Objective-C. The code is as follows :

I have a class MyClass whose header file contains :

@property (nonatomic) NSMutableDictionary *dict;

This gets initialized as follows :

_dict = [[NSMutableDictionary alloc] init];

Then I try to add keys to it :

- (void) addOrModify:(myKey*) key withValue:(myValue *) value {

[self.dict setObject:value forKey:key];

NSLog(@"Adding the key %@ %p with value %@ %p",[key description],key,[value description],value);

NSLog(@"%@",[self.dict description]);

}

And here is what I get upon adding different key/values :

Adding the key (key1) 0x7f9dc3590070 with value (value1) 0x7f9dc358e760

{

"(key1)" = "(value1)";

}

Adding the key (key2) 0x7f9dc357adc0 with value (value2) 0x7f9dc357de20

{

"(key2)" = (null);

"(key1)" = "(value1)";

}

Adding the key (key3) 0x7fa4d2cbdbd0 with value (value3) 0x7fa4d2d53cf0

{

"(key3)" = "(value3)";

"(key2)" = "(value2)";

"(key1)" = "(value1)";

}

Now if I take a look into the actual content of the dictionary with XCode's debugger, I find that all pairs key/value are present as they should. So it seems that the description method of dict fails to print correctly the value of some keys. The same happens without using NSLog but displaying the content of the dictionary in a UITable. Also note that if I try to log the address of the value for these particular keys, it also gives me 0x0 (so my description function is not at fault).

Note that the behavior is rather chaotic, sometimes several values are set to nil, then they appear correctly after I have added a few more pairs of key/values. I really can't get my head around this problem, any help would be much appreciated.

Edit 1

The interface of my keys

@interface myKey : NSObject <NSCopying>

@property(nonatomic, copy) NSString *myKeyName;

@property(nonatomic) float myKeyFloat;

@property(nonatomic, readonly, strong) NSDate *dateCreated;

The interface of my values:

typedef NS_ENUM(NSInteger, Unities) { g, kg, };

@interface myValue : NSObject

@property(nonatomic) float myValueFloat;

@property(nonatomic) Unities myValueUnit;

Calling code

- (IBAction)addKeyValue:(id)sender {

myValue *value = [myValue randomValue];

myKey *key =[[CatalogKeys sharedCatalogKeys]

getRandomKeyFromStore]; [self.dict addOrModify:key withValue: value];

}

where randomValue just set myValueUnit to 'g' and puts a random float in myValueFloat

网友答案:

This is a guess:

The information supplied does not show that your type myKey implements isEqual: and hash. Keys must implement these methods to work correctly in an NSDictionary; if they are not implemented the default NSObject implementations will be used and they probably do not produce the correct results for instances of myKey.

You also indicate that using the description of your myKey instances does work, this makes sense as description returns an NSString and that type implements isEqual: and hash.

HTH

分享给朋友:
您可能感兴趣的文章:
随机阅读: