(null) strips of bacon

0 views
Skip to first unread message

Ben Scholbrock

unread,
May 11, 2009, 2:14:35 PM5/11/09
to RTFM: Development for Mobile Devices
(google groups hates me)
Hey all,

I ran into this problem (subject) when adding numbers using "floatFromMixedNumberString".  Turns out it was parsing out empty strings (@"") and considering them as 0.0 as a float, which led to a divide-by-zero.

Long story short, I converted this:
NSArray *parts = [str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" /"]];

to this:
NSArray *tempParts = [str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" /"]];
NSMutableArray *parts = [NSMutableArray arrayWithArray:tempParts];
   
    int counter = ([parts count] - 1);
    float value;
    while (counter > 0) {
        value = [[parts objectAtIndex:counter] floatValue];
        NSLog(@"%f", [[parts objectAtIndex:counter] floatValue]);
        if (value < 1) {
            [parts removeObjectAtIndex:counter];
        }
        counter--;
    }
    //NSLog(@"parts:%@", parts);

to check for zeros before performing arithmetic (it's a hack, I know).

Just in case someone else gets (null) strips of bacon too...

-ben
Reply all
Reply to author
Forward
0 new messages