EXC_BAD_ACCESS(Code = EXC_ARM_DA_ALIGN)在使用BOOL创建NSDictionary时收到

时间:2014-03-13 16:30:26

标签: ios objective-c memory-management nsdictionary exc-bad-access

我正在尝试使用3个元素创建一个字典对象,其中一个元素是BOOL值。我收到一个EXC_BAD_ACCESS(Code = EXC_ARM_DA_ALIGN,address = 0x5),我使用[NSDictionary dictionaryWithObjectsAndKeys:object,key,nil]将BOOL存储为对象

以下代码有什么问题:

- (void) retrieveAchievmentMetadata
{
[GKAchievementDescription loadAchievementDescriptionsWithCompletionHandler:
 ^(NSArray *descriptions, NSError *error) {
     if (error != nil)
     {
         // Process the error.
     }
     if (descriptions != nil)
     {

         [self setAchievementDescriptions:[[NSMutableDictionary alloc] init]]; 

         for (GKAchievementDescription *achievementDescription in descriptions) {

             NSDictionary *achievementData = [NSDictionary dictionaryWithObjectsAndKeys:
                                              [achievementDescription achievedDescription], @"Achieved Description",
                                              [achievementDescription maximumPoints], @"Maximum Points",
                                              [NSNumber numberWithBool:FALSE], @"Level Is Completed", nil];

             [achievementDescriptions setObject:achievementData forKey:[achievementDescription identifier]];

         }
     }
 }];

}

上发生EXC_BAD_ACCESS错误
[NSNumber numberWithBool:FALSE], @"Level Is Completed", nil];

2 个答案:

答案 0 :(得分:7)

崩溃的原因是[achievementDescription maximumPoints]maximumPoints属性返回NSInteger而不是NSNumber个实例。

将其更改为: [NSNumber numberWithInteger:[achievementDescription maximumPoints]], @"Maximum Points", ...

答案 1 :(得分:1)

由于提到了字面语法:如果你写了

NSDictionary *achievementData = { 
    @"Achieved Description": achievementDescription.achievedDescription,
    @"Maximum Points": achievementDescription.maximumPoints, 
    @"Level Is Completed": @NO
};

然后它不仅更具可读性,而且编译器还会告诉你是否做了愚蠢的事情,比如试图将整数放入字典中。