在switch语句中命中默认值后EXC BAD ACCESS

时间:2012-05-07 14:46:12

标签: objective-c ios cocoa-touch exc-bad-access

我有这个简单的开关声明:

switch ([sender tag]) {
    case 0:
        [NVAlertView showNVAlertViewFromView:self.view 
                                       title:@"STOCK NUMBER" 
                                 description:@"XXXXXXXXXXX" 
                                 cancelTitle:@"Cancel" 
                                  otherTitle:@"DONE" 
                                dismissBlock:^(BOOL canceled, NSString *description) {
                                    if (!canceled) {
                                        [newItem setObject:description forKey:@"stocknum"];
                                    }
                                }];
        NSLog(@"YOP!");
        break;

    case kDescription:
        [NVAlertView showNVAlertViewFromView:self.view 
                                       title:@"DESCRIPTION" 
                                 description:@"Enter a description here." 
                                 cancelTitle:@"Cancel" 
                                  otherTitle:@"DONE" 
                                dismissBlock:^(BOOL canceled, NSString *description) {
                                    if (!canceled) {
                                        [newItem setObject:description forKey:@"description"];
                                    }
                                }];
        break;

    case kBrandName:
        [NVAlertView showNVAlertViewFromView:self.view 
                                       title:@"BRAND NAME" 
                                 description:@"Enter a brand name here." 
                                 cancelTitle:@"Cancel" 
                                  otherTitle:@"DONE" 
                                dismissBlock:^(BOOL canceled, NSString *description) {
                                    if (!canceled) {
                                        [newItem setObject:description forKey:@"description"];
                                    }
                                }];
        break;

    case kPrice:
        [NVAlertView showNVAlertViewFromView:self.view 
                                       title:@"PRICE" 
                                 description:@"$199.99" 
                                 cancelTitle:@"Cancel" 
                                  otherTitle:@"DONE" 
                                dismissBlock:^(BOOL canceled, NSString *description) {
                                    if (!canceled) {
                                        [newItem setObject:description forKey:@"description"];
                                    }
                                }];
        break;

    default:
        NSLog(@"default");
        break;
}

每次a值达到默认情况时,由于EXC BAD ACCESS,它会输出“default”并导致程序崩溃。是什么导致了这个? 谢谢!

2 个答案:

答案 0 :(得分:2)

EXC_BAD_ACCESS通常是由访问已经解除分配的对象引起的。

检查此切换块后行中的对象,以确保它们具有保留计数>我也会检查发件人。

答案 1 :(得分:0)

你在块中设置值的newItem的范围是什么?

当你的调用块被调用时,它可能超出了范围 - 记住你的警报视图被呈现并等待你的输入,但是代码一直在执行并留下你的switch语句(和方法,基于你的解释)

您可以设置另一个创建newItem并设置其值的方法,而不是直接在块中执行操作,并从dismissBlock中调用该特定方法。