Here is the code im having issues with:
if(DriveInfoDict) {
NSLog(@"%@", DriveInfoDict);
//PrevSp开发者_StackOverfloweedsDict = [DriveInfoDict objectForKey: @"speed"];
//NSLog(@"Previous Speed Dict:%@", PrevSpeedsDict);
}
DriveInfoDict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble: CurrentLatitude], @"Lat",
[NSNumber numberWithDouble: CurrentLongitude], @"Long",
[NSNumber numberWithDouble:speedMPH], @"speed",
nil];
Here, I would like to set DriveInfoDict, so that the next time the function runs it will have the previous value. I have stripped the operators to simplify my problem.
The error I am getting is : EXC-BAD-ACCESS
I am new to Obj-C and I do not know how to make this object accessible here. Some code with explanation as to if it goes in the H or M file would be very helpful.
You need to retain the dictionary or use alloc/init
(which returns a retained dictionary. So either:
DriveInfoDict = [[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble: CurrentLatitude], @"Lat",
[NSNumber numberWithDouble: CurrentLongitude], @"Long",
[NSNumber numberWithDouble:speedMPH], @"speed",
nil] retain];
or:
DriveInfoDict = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithDouble: CurrentLatitude], @"Lat",
[NSNumber numberWithDouble: CurrentLongitude], @"Long",
[NSNumber numberWithDouble:speedMPH], @"speed",
nil];
If you replace the content of DriveInfoDict
(that is: assign a new dictionary) don't forget to first release it.
精彩评论