【发布时间】:2014-01-16 01:51:00
【问题描述】:
当我在我的设备上测试应用程序时,我遇到了 [__NSArrayM insertObject:atIndex:]: object cannot be nil 崩溃,但在 iOS 模拟器上却没有。
这是怎么回事:
我在视图控制器上管理 5 个UITextField,然后我使用 IBAction(当我按下按钮时)通过 NSString 将每个 UITextField 的文本传递给 另一个视图控制器 ,它会崩溃)。
TextViewController
- (IBAction)choicebutton:(id)sender {
AnswerViewController *AVC = [self.storyboard instantiateViewControllerWithIdentifier:@"AnswerViewController"];
AVC.stringFromChoice1 = self.choice1.text;
AVC.stringFromChoice2 = self.choice2.text;
AVC.stringFromChoice3 = self.choice3.text;
AVC.stringFromChoice4 = self.choice4.text;
AVC.stringFromChoice5 = self.choice5.text;
[self presentViewController:AVC animated:YES completion:nil];
}
然后在 AnswerViewController 上,我正在创建一个 NSMutableArray 并将 NSStrings 随机化以显示在 UILabel 上。
AnswerViewController
- (void)viewDidLoad
{
self.choiceAnswers1 = [[NSMutableArray alloc] initWithCapacity:5];
if(![self.stringFromChoice1 isEqualToString:@""])
{
[self.choiceAnswers1 addObject:self.stringFromChoice1];
}
if(![self.stringFromChoice2 isEqualToString:@""])
{
[self.choiceAnswers1 addObject:self.stringFromChoice2];
}
if(![self.stringFromChoice3 isEqualToString:@""])
{
[self.choiceAnswers1 addObject:self.stringFromChoice3];
}
if(![self.stringFromChoice4 isEqualToString:@""])
{
[self.choiceAnswers1 addObject:self.stringFromChoice4];
}
if(![self.stringFromChoice5 isEqualToString:@""])
{
[self.choiceAnswers1 addObject:self.stringFromChoice5];
}
int index = arc4random() % [self.choiceAnswers1 count];
self.choiceanswer.text = self.choiceAnswers1[index];
self.choiceanswer1.text = self.choiceAnswers1[index];
}
我已经这样设置了,以防用户没有填写所有的 UITextFields,这对崩溃有什么影响吗?这个我搞不明白,求大神帮忙!
谢谢!
【问题讨论】:
-
那么它在哪里崩溃了?
-
当我触发 IBAction 时它崩溃了
标签: ios objective-c nsstring nsmutablearray