将NSString添加到NSMutableArray崩溃中



在我的iOS应用程序上,我有一个带有两件事的UI:picker视图和一个按钮。

我用这样的nsmutablearray初始化我的选择器视图:

@interface ViewController ()
{
    NSMutableArray *_pickerDataCategory;
}
- (void)viewDidLoad
{
   // Initialize Data
    _pickerDataCategory = [[NSMutableArray alloc] init];
    NSMutableArray  *array = [NSMutableArray arrayWithObjects:
                            @"cat1", @"cat2", @"cat3", @"cat4", nil ];
    _pickerDataCategory = array;
 //First I had initialize like this :  NSMutableArray  *_pickerDataCategory = [NSMutableArray arrayWithObjects:
                            @"cat1", @"cat2", @"cat3", @"cat4", nil ];
}

然后,我有一个按钮显示弹出窗口,用户可以在其中写东西。此方法的目的是添加一个新的NSString对象,

   - (IBAction)addNewCategory:(id)sender {
        __block bool isNotExist = false;
           UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Add new category"
                                                                                  message: @"Please write a new category"
                                                                           preferredStyle:UIAlertControllerStyleAlert];
        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.placeholder = @"Category";
            textField.textColor = [UIColor blueColor];
            textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            textField.borderStyle = UITextBorderStyleRoundedRect;
        }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [alertController dismissViewControllerAnimated:YES completion:nil];
    }];
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSString *input = alertController.textFields[0].text;
        //Check if the category exist already
        for (NSString *category in _pickerDataCategory)
        {
            if ([category isEqualToString:input])
            {
                 [self popupMessage:@"Category already exists" title:@"Error"];
                isNotExist = false;
                return;
            }else{
                NSString *msg = [@"Category has been added : " stringByAppendingString:input];
                 [self popupMessage:msg title:@"Ok"];
                isNotExist = true;

                //[_pickerDataCategory addObject:input];//CRASH
              //  [_picker_cateogry reloadAllComponents];
            }
        }

    }];
    [alertController addAction:cancel];
    [alertController addAction:ok];
    [self presentViewController:alertController animated:YES completion:nil];
}

,但是当我想将TextField输入添加到我的NSMutableArray时,它崩溃了。我真的不明白为什么。我对Nsmutablearray的初始化感到非常谨慎。我在Internet上寻找有关它的任何信息,我发现的两个信息是关于初始化的,或者如果它是可变的数组而不是简单的数组。

此外,我不明白为什么如果我在[self presentViewController:alertController animated:YES completion:nil];之后添加一些代码,它没有执行...我没有找到任何信息。

您能帮我弄清楚为什么它崩溃而它不起作用吗?谢谢

就崩溃而言,它可能与您在枚举时试图修改数组有关。我会把这些分解为两个单独的任务。

类似...

NSMutableArray *additionalObjects = [[NSMutableArray alloc] init];
for (NSString *category in _pickerDataCategory) {
    if (![category isEqualToString:input]) {
        [additionalObjects addObject: category];
    }
}
[_pickerDataCategory addObjectsFromArray: additionalObjects];

问题是,您正在尝试在迭代时将值添加到数组中,将数据保存在其他数组中,然后将其添加到_pickerdataCategory

代码将是这样的: - 枚举

    - (IBAction)addNewCategory:(id)sender {
    __block bool isNotExist = false;
       UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Add new category"
                                                                              message: @"Please write a new category"
                                                                       preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"Category";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
    }];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    [alertController dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    NSString *input = alertController.textFields[0].text;
    //Check if the category exist already
    NSMutableArray * arrTemp = _pickerDataCategory;
    for (NSString *category in arrTemp)
    {
        if ([category isEqualToString:input])
        {
             [self popupMessage:@"Category already exists" title:@"Error"];
            isNotExist = false;
            return;
        }else{
            NSString *msg = [@"Category has been added : " stringByAppendingString:input];
             [self popupMessage:msg title:@"Ok"];
            isNotExist = true;

            [_pickerDataCategory addObject:input];//CRASH
            [_picker_cateogry reloadAllComponents];
        }
    }

}];
[alertController addAction:cancel];
[alertController addAction:ok];
[self presentViewController:alertController animated:YES completion:nil];
  }

最新更新