I am a relatively new iPhone/objective-c programmer and I have an issue that makes me feel quite out of my element.
I am trying to use a class method that creates a dictionary which I try to access in other methods. But the dictionary is…for lack of a better term — trippin’.
Here is my code in DynamoDBManager.m (the parts that I think are important):
static NSMutableDictionary *voiceMap = nil;
@implementation DynamoDBManager
+(NSMutableDictionary *)createVoiceMap{
NSLog(@"creating voice map...");
if (voiceMap == nil){
NSLog(@"Voice Map was nil");
voiceMap = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
@"piano", @"trumpet", @"hi_hat1", @"p", @"t", @"h",
@"acquitar", @"bass_drum1", @"clarinet", @"hi_tom", @"mid_tom", @"low_tom", @"crash_cymbal1", @"ride_cymbal1", @"snare_drum1", @"snare_drum2", @"violin",
@"g", @"b", @"c", @"i", @"m", @"l", @"y", @"r", @"s", @"a", @"v", nil]
forKeys:[NSArray arrayWithObjects:
@"p", @"t", @"h", @"piano", @"trumpet", @"hi_hat1",
@"g", @"b", @"c", @"i", @"m", @"l", @"y", @"r", @"s", @"a", @"v",
@"acquitar", @"bass_drum1", @"clarinet", @"hi_tom", @"low_tom", @"mid_tom", @"crash_cymbal1", @"ride_cymbal1", @"snare_drum1", @"snare_drum2", @"violin", nil]];
}
NSLog(@"done creating voice map");
return voiceMap;}
+(NSString *)generateNoteCipherGivenX:(int)x Y:(int)y andNote:(id)theNote{
NSLog(@"%@ -- voiceMap", voiceMap);
NSString *voiceCipherString = [[DynamoDBManager createVoiceMap] objectForKey:[theNote getVoice]];
if ([theNote isKindOfClass:[PitchedNote class]]) {
int duration2 = [theNote getDuration];
NSString *cipher = [NSString stringWithFormat:@"%@%i%i%i,", voiceCipherString, y+10, duration2, x];
return cipher;
}
else{
NSString *cipher = [NSString stringWithFormat:@"%@%i%i%i,", voiceCipherString, y, 0, x];
return cipher;
}
}
In another method in DynamoDBManager, I call
[DynamoDBManager generateNoteCipherGivenX:curX Y:curY andNote:curNote];
It has logged some strange errors, including, on three different runs of the app:
2012-08-10 21:53:48.090 createAccount[537:207] __NSCFDictionary -- type
2012-08-10 21:53:48.091 createAccount[537:207] {
IOProviderClass = IOFireWireUnit;
"Unit_SW_Version" = 16;
"Unit_Spec_ID" = 2599;
} -- voiceMap
2012-08-10 21:57:55.635 createAccount[580:207] __NSMallocBlock__ -- type
2012-08-10 21:57:55.636 createAccount[580:207] <__NSMallocBlock__: 0x810cb50> -- voiceMap
2012-08-10 22:38:10.044 createAccount[1188:207] -[__NSMallocBlock__ objectForKey:]: unrecognized selector sent to instance 0x77700e0
2012-08-10 22:38:10.044 createAccount[1188:207] Exception: -[__NSMallocBlock__ objectForKey:]: unrecognized selector sent to instance 0x77700e0
I must be doing something incredibly wrong to get such erratic behavior. What is it?
I bet you are not using ARC (Automatic Reference Counting) so you should retain you dictionary:
Since you don’t retain the dictionary, it is deallocated soon after and the memory
voiceMapis pointing to is very likely reused for other objects.