1

我是一个相对较新的 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

我必须做一些非常错误的事情才能得到这种不稳定的行为。它是什么?

4

1 回答 1

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指向的内存很可能会被其他对象重用。

于 2012-08-11T06:00:30.913 回答