我是一个相对较新的iPhone / objective-c程序员,我有一个问题让我觉得我的元素非常不合适。
我正在尝试使用一个类方法来创建一个我尝试在其他方法中访问的字典。但这本词典是......由于缺乏一个更好的术语 - trippin'。
这是我在DynamoDBManager.m中的代码(我认为重要的部分):
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;
}
}
在DynamoDBManager的另一个方法中,我调用
[DynamoDBManager generateNoteCipherGivenX:curX Y:curY andNote:curNote];
它记录了一些奇怪的错误,包括在三个不同的应用程序运行中:
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
我必须做某事非常错误才能获得这种不稳定的行为。它是什么?
答案 0 :(得分:2)
我打赌你没有使用ARC(自动引用计数)所以你应该保留字典:
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]] retain];
由于您没有保留字典,因此很快就会释放它,并且内存voiceMap
指向的内存很可能会重复用于其他对象。