Всякий раз, когда я пытаюсь сохранить NSUserDefualts в NSMutableArray, происходит сбой - PullRequest
0 голосов
/ 06 июля 2019

Я использовал 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 голосов
/ 06 июля 2019

в конце концов понял это, довольно просто, теперь, когда я думаю об этом, и я чувствую себя довольно глупо, не зная об этом раньше, лол ...

    tableData = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"tabelSave"]];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...