Ios 将NSString添加到NSMutableArray崩溃
在我的iOS应用程序上,我有一个包含两件事的UI:一个选择器视图和一个按钮 我使用NSMutableArray初始化我的选择器视图,如下所示:Ios 将NSString添加到NSMutableArray崩溃,ios,objective-c,arrays,ipad,nsmutablearray,Ios,Objective C,Arrays,Ipad,Nsmutablearray,在我的iOS应用程序上,我有一个包含两件事的UI:一个选择器视图和一个按钮 我使用NSMutableArray初始化我的选择器视图,如下所示: @interface ViewController () { NSMutableArray *_pickerDataCategory; } - (void)viewDidLoad { // Initialize Data _pickerDataCategory = [[NSMutableArray alloc] init];
@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]之后添加一些代码代码>,它不会被执行。。。我也没有找到关于它的任何信息
你能帮我弄清楚它为什么会坏掉而不能工作吗?
感谢就您的崩溃而言,这可能与您在枚举数组时试图修改数组有关。我会把它们分成两个独立的任务
类似于
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];
}
你能把你收到的紧急消息也加上吗?将使查找问题更容易一些!:)@瑞克,是的,我忘了。我只是编辑我的帖子;)感谢在试图将对象添加到数组中的位置放置一个断点,并检查值是否为nil。实际上,即使我写入[\u pickerDataCategory addObject:@“Test”],它也会崩溃……因此我认为这不是值为nil。不,不是。我刚检查过。如果它是零,它会显示一条错误消息,而我什么也没有得到…很好的观察MaratThank!我只是试着像你说的那样分两次做。一旦用户写了一些东西,就会存储NSString,然后在循环之后,我将其添加到NSMutableArray中。你知道为什么吗,如果我在presentViewController后面放了一些代码,它就不会执行了?只需将枚举数组替换为temp即可