0

我有一个奇怪的崩溃。我从现有应用程序创建了一个新应用程序......并将其从 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];
}

希望这对某人有帮助!

4

1 回答 1

1

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

于 2013-07-27T20:35:41.643 回答