Reputation: 365
I am relatively new to Objective-C and now I have a problem in my iPhone app that I don't fully understand.
I try to use a NSMutableDictionary
, this does not seem to work as i expect for some reason. When I run the debugger and do po numberToCallerMap
to see the dictionary, I get an exception. I have read the documentation for NSMutableDictionary
on how to initialize it, but I can not see what I am doing wrong. Help and advice are appreciated. The variable causing me problem is numberToCallerMap, here is the relevant function:
- (void)setData:(NSString*)value{
[list release];
list = [[NSMutableArray alloc] init];
SBJSON *json = [[[SBJSON alloc] init] autorelease];
NSMutableDictionary* numberToCallerMap;
CallerInfo* caller;
NSDictionary* callerInfo;
@try {
NSArray *array = (NSArray*)[json objectWithString:value];
// reading all the items in the array one by one
numberToCallerMap = [NSMutableDictionary dictionary];
for (id *item in array) {
// if the item is NSDictionary (in this case ... different json file will probably have a different class)
NSDictionary *dict2 = (NSDictionary *) item;
CallInfo *data = [CallInfo alloc];
[data initFromDictionary:dict2];
callerInfo = (NSDictionary*)[dict2 valueForKey:@"caller"] ;
//Here, we want the phonenumber to be part of the CallerInfo object instead.
// It is sent from the server as part of the Call-object
NSString* number = (NSString*)[dict2 valueForKey:@"phoneNumber"];
[callerInfo setValue:number forKey:@"phoneNumber"];
caller = (CallerInfo*)[numberToCallerMap valueForKey:number];
if(caller == nil || [caller isKindOfClass:[NSNull class]]){
caller = [CallerInfo alloc];
[caller initFromDictionary:callerInfo];
[numberToCallerMap setValue:caller forKey:number];
[list insertObject:caller atIndex:0];
}
[caller addRecentCall:data];
}
}
@catch (NSException * e) {
[list release];
list = [[NSMutableArray alloc] init];
}
@finally {
[numberToCallerMap release];
}
}
Upvotes: 0
Views: 3541
Reputation: 9543
This is probably not the only problem, but you are not alloc
-ing your numberToCallerMap
dictionary, you are getting it from a convenience class method -- [NSMutableDictionary dictionary]
-- that returns it autoreleased. So you should not call release
on it yourself.
Upvotes: 1