gpt4 book ai didi

ios - 我无法弄清楚这种崩溃:[__NSArrayM insertObject:atIndex:]:对象不能为nil

转载 作者:行者123 更新时间:2023-12-01 19:03:48 24 4
gpt4 key购买 nike

我在设备上而不是在iOS模拟器上测试应用程序时, [__NSArrayM insertObject:atIndex:]:对象不能为崩溃。

这是怎么回事:

我在一个View Controller上管理5个UITextField,然后使用IBAction通过NSString将每个UITextField的文本传递给另一个View Controller 的(当我按下按钮时,它崩溃了)。

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

我已经以这种方式进行了设置,以防用户没有填写所有UITextField,这与崩溃有关系吗?我不知道这一点,请帮忙!

谢谢!

最佳答案

更改您的viewDidLoad这样的东西。

- (void)viewDidLoad
{
self.choiceAnswers1 = [[NSMutableArray alloc] init];

if(self.stringFromChoice1.length > 0)
{
[self.choiceAnswers1 addObject:self.stringFromChoice1];
}
if(self.stringFromChoice2.length > 0)
{
[self.choiceAnswers1 addObject:self.stringFromChoice2];
}
if(self.stringFromChoice3.length > 0)
{
[self.choiceAnswers1 addObject:self.stringFromChoice3];
}
if(self.stringFromChoice4.length > 0)
{
[self.choiceAnswers1 addObject:self.stringFromChoice4];
}
if( self.stringFromChoice5.length > 0)
{
[self.choiceAnswers1 addObject:self.stringFromChoice5];
}
int index = arc4random() % [self.choiceAnswers1 count];
self.choiceanswer.text = self.choiceAnswers1[index];
self.choiceanswer1.text = self.choiceAnswers1[index];
}

让我知道这会有所帮助.. :)

关于ios - 我无法弄清楚这种崩溃:[__NSArrayM insertObject:atIndex:]:对象不能为nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21152001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com