崩溃在[arr addObject:row]; (iPhone开发)

时间:2011-04-08 13:32:07

标签: iphone objective-c cocoa-touch

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if ([[data objectForKey:@"rows"] count] == indexPath.row) {
        UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:@"Kies wat u wilt toevoegen" delegate:self cancelButtonTitle:@"Annuleren" destructiveButtonTitle:nil otherButtonTitles:@"Veld", @"Afbeelding", @"Locatie",nil];
        [actionsheet showInView:self.view];
        [actionsheet release];
    }

}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != 3) {
        self.temp = buttonIndex;
        UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Nieuw element" message:@"Kies een naam voor het toe te voegen element" delegate:self cancelButtonTitle:@"Annuleren" otherButtonTitles:@"Ok", nil];
        [myAlert addTextFieldWithValue:nil label:@"Naam voor element"];
        [[myAlert textField] setTextAlignment:UITextAlignmentCenter];
        [[myAlert textField] becomeFirstResponder];
        [myAlert show];
        [myAlert release];
    }
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {

        int actionSheetButtonIndex = self.temp;
        self.temp = nil;

        //Add row
        NSString *typed = [[alertView textField] text];

        if (actionSheetButtonIndex == 0 || actionSheetButtonIndex == 2) { //If field or location
            NSMutableArray *arr = [data objectForKey:@"rows"];
            if (arr == nil) {
                arr = [NSMutableArray array];
            }
            NSMutableDictionary *row = [NSMutableDictionary dictionary];
            [row setValue:typed forKey:@"name"];
            [row setValue:@"" forKey:@"value"];
            [arr addObject:row];
            [data setObject:arr forKey:@"rows"];

            [self updateUserDefaults];
        }


    }
}

我有这段代码,但应用程序在到达didDismissWithButtonIndex部分时崩溃了。我发现当我评论[arr addObject:row]时它不会崩溃;部分代码,但我无法弄清楚它有什么问题。

控制台说中止陷阱。我认为这也不是很有用。

编辑:我一直在尝试一些事情,没有结果。我现在有以下代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if ([[data objectForKey:@"rows"] count] == indexPath.row) {
        UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:@"Kies wat u wilt toevoegen" delegate:self cancelButtonTitle:@"Annuleren" destructiveButtonTitle:nil otherButtonTitles:@"Veld", @"Afbeelding", @"Locatie",nil];
        [actionsheet showInView:self.view];
        [actionsheet release];
    }

}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != 3) {
        self.temp = buttonIndex;
        UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Nieuw element" message:@"Kies een naam voor het toe te voegen element" delegate:self cancelButtonTitle:@"Annuleren" otherButtonTitles:@"Ok", nil];
        [myAlert addTextFieldWithValue:nil label:@"Naam voor element"];
        [[myAlert textField] setTextAlignment:UITextAlignmentCenter];
        [[myAlert textField] becomeFirstResponder];
        [myAlert show];
        [myAlert release];
    }
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {

        int actionSheetButtonIndex = self.temp;
        self.temp = 0;

        //Add row
        NSString *typed = [[alertView textField] text];

        if (actionSheetButtonIndex == 0 || actionSheetButtonIndex == 2) { //If field or location
            NSMutableArray *oldarr = [[[data objectForKey:@"rows"] mutableCopy] autorelease];
            NSMutableArray *arr = [NSMutableArray array];

            for (int i = 0; i < [oldarr count]; i++) {
                [arr addObject:[oldarr objectAtIndex:i]];
            }

            NSMutableDictionary *row = [NSMutableDictionary dictionary];
            [row setObject:typed forKey:@"name"];
            [row setObject:@"" forKey:@"value"];
            //[arr addObject:row];
            NSMutableDictionary *dataCopy = data;
            [dataCopy setObject:arr forKey:@"rows"];
            self.data = dataCopy;

            [self updateUserDefaults];
        }


    }
}

头文件是:

#import <UIKit/UIKit.h>


@interface InfoViewController : UITableViewController <UIAlertViewDelegate, UIActionSheetDelegate> {

    UIView *images;
    NSMutableDictionary *data;
    int temp;

}

@property (nonatomic, retain) UIView *images;
@property (nonatomic, retain) NSMutableDictionary *data;
@property (nonatomic) int temp;

@end

这是updateUserDefaults方法:

- (void)updateUserDefaults {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary *tempdict = [defaults objectForKey:@"data"];
    [tempdict setObject:self.data forKey:@"rows"];
    [defaults setObject:tempdict forKey:@"data"];
}

现在控制台为我提供了更多输出:

Console output

You can download the crash log mentioned by the console here

我真的希望你能帮助我!

2 个答案:

答案 0 :(得分:2)

确保arr实际上是NSMutabableArray,当您从数据字典中检索它时,它可能只是一个NSArray。看看这是否有帮助:

NSMutableArray *arr = [[[data objectForKey:@"rows"] mutableCopy] autorelease];

编辑:

好的,看看你的崩溃日志,你似乎以某种方式在你的userdefaults中存储了一个NSArray,现在正试图以字典的形式检索它:

  • 检查应用程序中的所有位置,您没有为某个地方的密钥“数据”存储NSArray
  • 从模拟器中删除应用程序,因此可能仍然存在的旧设置被删除
  • 正如Joe所提到的,从用户默认值中检索到的对象不可变,所以请改为:

    NSMutableDictionary * tempdict = [[[defaults objectForKey:@“data”] mutableCopy] autorelease];

答案 1 :(得分:0)

你能试试吗

arr = [[NSMutableArray alloc] init];

而不是

arr = [NSMutableArray array];