启动时iOS断言失败

时间:2013-07-26 13:00:52

标签: nsarray components uicolor assertion ios6.1

我有一个奇怪的崩溃。我从现有的应用程序创建一个新的应用程序...并在从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方法,我只能在这个新应用程序上解决此错误。我想知道这里的错误是什么!

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

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

希望这有助于某人!

1 个答案:

答案 0 :(得分:1)

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