Read assert reference, add a breakpoint before assert (or even NSLog(@"%@", components);) and see what's happening ;-)
iOS Assertion failure upon startup
-
04-06-2022 - |
Question
I have a strange crash. I creating a new app from an existing app... and in altering it from NSDocumentsDirectory to NSApplicationSupportDirectory, I get an Assertion Failure on code that runs quite well before touching the methods that are associated with the NSDcoumentDirectory. The code that has the failure is in a color model.
+(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];
}
As can be seen... there are four components. They are all listed as separate properties. This is the error in the log...
Assertion failed: ([components count] == 4), function +[CAG_Color colorFromDashSeperatedStringRepresentation:], file /Users/marc/Documents/CAG/iClassNotes/iClassNotes/iClassNotes/CAG_Color.m, line 77.
Any help in this would be GREATLY appreciated!!! I have only been able to get around this error on this new app by porting all the controllers into a newly created project and not touching the NSDocumentDirectory methods. I'd like to know what the h**l is wrong here!
This is the solution that allowed me to give the array values, and to satisfy the assert, which is called numerous times... my app is a text app and the color model is used a lot.
+(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];
}
Hope this helps someone!
Solution