I’m having a strange problem while using NSDictionnary …
I’m trying to retrieve an object for a key that is present is the dictionnary with the method objectForKey, but it returns nil instead.
When I print out the whole dictionnary, I can see clearly the key and the value I’m looking for.
Here is the code :
- (MObject *)GetWithMProperty:(MProperty *)prop {
NSLog(@"We search an object for a property named %@", prop.Name);
NSArray *keyArray = [_dict allKeys];
int count = [keyArray count];
for (int i=0; i < count; i++) {
MObject *tmp = [_dict objectForKey:[ keyArray objectAtIndex:i]];
NSLog(@"Key = %@ | Object = %d", ((MProperty*)[keyArray objectAtIndex:i]).Name, tmp.GetTypeId);
if (prop == [keyArray objectAtIndex:i])
NSLog(@"Wouhou !");
else
NSLog(@"Too bad :(");
}
return [_dict objectForKey:prop];
}
And the stack trace :
2012-10-29 11:24:07.730 IOS6[1451:11303] We search an object for a property named Value
2012-10-29 11:24:07.730 IOS6[1451:11303] Key = Name | Object = 4
2012-10-29 11:24:07.731 IOS6[1451:11303] Too bad :(
2012-10-29 11:24:07.731 IOS6[1451:11303] Key = Value | Object = 0
2012-10-29 11:24:07.732 IOS6[1451:11303] Too bad :(
It’s a bit complicated, I’m using J2ObjC to compile a fully functional Engine, and thus I can’t modify the classes MProperty and MObject (used by the Engine).
MProperty doesn’t conforms to NSCopying protocol, so I created a classe called IPhoneMProperty that inherits from MProperty and conforms to the protocol.
Here is this class :
@implementation IPhoneMProperty
- (id)initWithMProperty:(MProperty *)prop {
self = [super initWithInt:prop.OwnerTypeId withNSString:prop.Name withInt:prop.TypeId withMBasicValue:prop.DefaultValue withInt:prop.Flags];
return self;
}
- (id)copyWithZone:(NSZone *)zone {
IPhoneMProperty *prop = [[IPhoneMProperty alloc] initWithMProperty:self];
return prop;
}
@end
And the method I use to add object and keys to the dictionnary :
- (void)SetWithMProperty:(MProperty *)prop withMObject:(MObject *)obj {
IPhoneMProperty *tempKey = [[IPhoneMProperty alloc] initWithMProperty:prop];
[_dict setObject:obj forKey:tempKey];
}
I hope it’s clear enough, actually it’s the only solution I found for the moment, but it doesn’t work 🙁
Can anyone helps me with this ?
Thanks !
The problem exists in the line
Instead, implement
isEquals:method in yourMPropertyclass.And, here, instead of the line
use the following line,