将NSString添加到NSMutableArray崩溃

时间:2016-12-07 15:30:08

标签: ios objective-c arrays ipad nsmutablearray

在我的iOS应用程序中,我有一个包含两个内容的UI:选择器视图和按钮。

我使用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对象添加到我的NSMutableArray

   - (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的初始化很谨慎。我在互联网上查找了有关它的任何信息,我发现的两个信息是关于初始化,或者它是一个可变数组而不是一个简单的数组。

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

你能帮我弄清楚它崩溃的原因吗?它不起作用? 感谢

3 个答案:

答案 0 :(得分:3)

就你的崩溃而言,它可能与你尝试修改阵列时的相关性有关。我会把它们分成两个单独的任务。

像...一样的东西。

NSMutableArray *additionalObjects = [[NSMutableArray alloc] init];

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

[_pickerDataCategory addObjectsFromArray: additionalObjects];

答案 1 :(得分:3)

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

答案 2 :(得分:3)

代码如下: - 枚举

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

  }