【问题标题】:I can't figure this crash out: [__NSArrayM insertObject:atIndex:]: object cannot be nil我无法弄清楚这个崩溃:[__NSArrayM insertObject:atIndex:]: object cannot be nil
【发布时间】: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


【解决方案1】:

不要对空字符串使用compare:——它不会捕捉到你的字符串是nil而不是@“”的情况。这是两种截然不同的情况。

而不是这个:

if(![self.stringFromChoice1 isEqualToString:@""])
{
    [self.choiceAnswers1 addObject:self.stringFromChoice1];
}

使用这个:

if (self.stringFromChoice1.length)
    [self.choiceAnswers1 addObject:self.stringFromChoice1];

由于在 C 中任何非 0 值都是 true,并且由于向 nil 对象发送消息总是返回 0,所以这可以捕获所有情况。并且不那么啰嗦。

代码越少越好!

【讨论】:

  • 只有我会把{} 留在里面。由于缺少{},对if 语句进行“快速、简单的编辑”的案例太多了。
  • 我禁止在我的应用程序中使用它们。我理解你的论点,但我的反驳始终是 Xcode 真的很擅长缩进,你应该把它打开,如果你用糟糕的缩进提交,我无论如何都会刺伤你。我的新员工正在接受培训。额外的线条和符号,BLEH。
  • 当我在设备上测试它时,它仍然给我同样的崩溃,但它在模拟器上运行良好。
  • 你确定你已经重建了模拟器版本吗?它们是不同的二进制文件。它在同一个地方崩溃吗?您能否发布堆栈跟踪(在调试器中键入“bt”并复制/粘贴前 15 行左右)。
  • 必须是if (self.stringFromChoice1.length)! 与您想要的相反。
【解决方案2】:

改变你 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];
}

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

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    相关资源
    最近更新 更多