【问题标题】:iOS Assertion failure upon startupiOS 断言在启动时失败
【发布时间】:2013-07-26 13:00:51
【问题描述】:

我有一个奇怪的崩溃。我从现有应用程序创建了一个新应用程序......并将其从 NSDocumentsDirectory 更改为 NSApplicationSupportDirectory,在触及与 NSDcoumentDirectory 关联的方法之前,我在运行良好的代码上遇到了断言失败。 有故障的代码在颜色模型中。

+(UIColor *) colorFromDashSeperatedStringRepresentation: (NSString *) dashSeperatedStr
{
    NSArray * components = [dashSeperatedStr componentsSeparatedByString:@"#"];

    assert([components count] == 4); // Assertion failure here... 

    float red = [[components objectAtIndex:0] floatValue];
    float green = [[components objectAtIndex:1] floatValue];
    float blue = [[components objectAtIndex:2] floatValue];
    float alpha = [[components objectAtIndex:3] floatValue];

    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

可以看出...有四个组件。它们都被列为单独的属性。这是日志中的错误...

Assertion failed: ([components count] == 4), function +[CAG_Color colorFromDashSeperatedStringRepresentation:], file /Users/marc/Documents/CAG/iClassNotes/iClassNotes/iClassNotes/CAG_Color.m, line 77.

对此的任何帮助将不胜感激!!!我只能通过将所有控制器移植到新创建的项目中而不触及 NSDocumentDirectory 方法来解决这个新应用程序上的这个错误。我想知道这里的 h**l 有什么问题!

这是允许我给出数组值并满足断言的解决方案,它被调用了很多次......我的应用程序是一个文本应用程序,并且颜色模型被使用了很多。

+(UIColor *) colorFromDashSeperatedStringRepresentation: (NSString *) dashSeperatedStr
{
    NSArray * components = [dashSeperatedStr componentsSeparatedByString:@"#"];
    NSLog(@"Components before assert: %@", components);
    if (components == NULL)
    {
        NSArray *values = @[@0.000000, @0.000000, @1.000000, @0.000000];
        components = values;
    }
    NSLog(@"Components after if: %@", components);
    assert([components count] == 4);

    float red = [[components objectAtIndex:0] floatValue];
    float green = [[components objectAtIndex:1] floatValue];
    float blue = [[components objectAtIndex:2] floatValue];
    float alpha_c = [[components objectAtIndex:3] floatValue];

    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha_c];
}

希望这对某人有所帮助!

【问题讨论】:

    标签: nsarray components uicolor assertion ios6.1


    【解决方案1】:

    阅读assert reference,在断言之前添加一个断点(甚至是 NSLog(@"%@", components);) 看看发生了什么;-)

    【讨论】:

    • 欣赏答案...不知道为什么我没有检查数组的值。当然,它们显示为空。呸!难怪断言失败了。这不是我真正想要的答案,也没有解决问题,但是,它让我走上了正确的道路,以找出真正的问题是什么......“为什么数组是空的?”
    • 在做了一些进一步的研究并尝试了一些事情之后,我发现了 3 年前的一篇关于 NSArrays 的帖子。它帮助我找到了解决这个问题的途径。我添加了一个提供解决方案的编辑。
    猜你喜欢
    • 2014-10-03
    • 1970-01-01
    • 2012-09-17
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    相关资源
    最近更新 更多