开发者

Crash at [arr addObject:row]; (iPhone development)

- (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];
        }


    }
}

I have this code, but the app crashes as it reaches the didDismissWithButtonIndex part. I figured out it doesn't crash when I comment the [arr addObject:row]; part of the code, but I can't figure out what's wrong with it.

Console says Abort trap. That's not very useful either, I think.

EDIT: I've been trying some things for a while, with no result. I have the following code right now:

- (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];
        }


    }
}

The header file is:

#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

And here's the updateUserDefaults method:

- (void)updateUserDefaults {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary *tempdict = [defaults objectForKey:@"data"];
  开发者_运维技巧  [tempdict setObject:self.data forKey:@"rows"];
    [defaults setObject:tempdict forKey:@"data"];
}

Now the console gives me some more output:

Crash at [arr addObject:row]; (iPhone development)

You can download the crash log mentioned by the console here

I really hope you can help me out!


Make sure arr is in fact a NSMutabableArray, when you retrieve it from your data Dictionary it might just be a NSArray. See if this helps:

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

EDIT:

okay looking at your crashlog, it seems you somehow stored a NSArray in your userdefaults and are now trying to retrieve it as Dictionary you should:

  • check all places in the App, that you don't store an NSArray for the key "data" somewhere
  • delete the app from the simulator, so old settings that maybe still there get wiped
  • As Joe mentioned, retrieved Objects from the userdefaults are not mutable, so do this instead:

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


Can you try this

arr = [[NSMutableArray alloc] init];

Instead of

arr = [NSMutableArray array];

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜