每当我尝试将NSUserDefualts保存到NSMutableArray时,它就会崩溃

时间:2019-07-05 21:35:17

标签: ios objective-c xcode

我一直在使用NSUserDefualts。每当我尝试保存到NSMutableArray时,应用程序就会崩溃。我该如何解决? 我试图在网上找到答案,但它一直在迅速提出或不支持我搜索到的任何内容的指南。

#import "scoutViewViewController.h"

@interface scoutViewViewController ()
@property (weak, nonatomic) IBOutlet UITableView *scoutView;


@end
BOOL check = YES;

@implementation scoutViewViewController 

*tableData; // u need this for a standalone/ static one


- (void)viewDidLoad {
         [super viewDidLoad];
         // Do any additional setup after loading the view.

        tableData =[[NSUserDefaults standardUserDefaults] objectForKey:@"data_0"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    return cell;
}
//segue select
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"show" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"show"]) {

    }
}
//ediatable tableView
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        [tableData removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self scoutView];


    }
}

- (IBAction)addCell:(id)sender {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Team Name Or Number" message: @"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert show];

}
-(void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    //only accept if the user hit ok
    // need to implement "UIAlertController" becuase UIAlert is no longer used.
    if(buttonIndex == 1){

        NSString *temptxtfield = [alertView textFieldAtIndex:0].text;

        if(!tableData){
            tableData = [[NSMutableArray alloc]init];
        }

      if([temptxtfield  isEqual: @""] || [temptxtfield  isEqual: @" "]){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Team Name Or Number" message: @"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
            alert.alertViewStyle = UIAlertViewStylePlainTextInput;
            check = NO; //check, nothing to do wiht the implemtaiont

        }else{
            check = YES;//check, nothing to do wiht the implemtaiont

        }
        if(check == YES){//check, nothing to do wiht the implemtaiont

            [tableData insertObject:temptxtfield atIndex:0];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
            [self.scoutView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];



            check= NO;//check, nothing to do wiht the implemtaiont
        }//check, nothing to do wiht the implemtaiont
        //check is to prevent nothing to be placed into the tabel, nothing to do with how the data is inserted into the table

    }

}

- (IBAction)saveTable:(id)sender {
    [[NSUserDefaults standardUserDefaults] setObject: tableData forKey:@"data_0"];

}

-(void)save2{

}


@end

当它崩溃时,它给了我一个SIGBART错误,我试图找出断点,但这很痛苦。

1 个答案:

答案 0 :(得分:0)

最终弄清楚了,现在我想起来很简单,而且在大声笑之前不知道它,我感到很愚蠢...

    tableData = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"tabelSave"]];
相关问题