Reputation: 3704
I've got this loop in Objective C and need some coaching to get it to work out. I'm trying to calculate the height of four bars based on the numbers stored as strings in array, array. biggestTotal is a string containing the largest of the four objects in array. I previously formatted each of the elements in array as currency.
for (int j=0; j<[array count]; j++) {
NSLog(@"%@ %@ %@",[NSString stringWithFormat:@"%d", j], [array objectAtIndex:j], biggestTotal);
double barHeight = (([[array objectAtIndex:j] doubleValue])*400.0/([biggestTotal doubleValue]));
NSLog(@"%@",barHeight);
CGRect currentRect = CGRectMake((150.0+(j*125.0)), (425.0-barHeight), 100.0, barHeight); // (x, y, width, height)
CGContextAddRect(context, currentRect);
CGContextDrawPath(context, kCGPathFillStroke);
}
With my NSLog statements above, I get Console outputs as follows:
0 $106,758.20 $106,758.20
(null)
1 $14,536.69 $106,758.20
(null)
2 $32,111.96 $106,758.20
(null)
3 $100,020.00 $106,758.20
(null)
Also, sadly I get no bars. What am I missing here? Why is the barHeight a (null)? Any help would be most appreciated...
Upvotes: 1
Views: 3031
Reputation: 64002
barHeight
is NULL
* because @"$106,758.20"
doesn't have a valid doubleValue
. Get rid of the $
and the ,
** and it should work.
Also, if you're doing math with currency, don't use double
. Use NSDecimalNumber
*Actually 0.0
because it can't be converted, per the docs, but then, as everyone else has pointed out, you're trying to log it with the wrong format specifier: %@
, which is for objects, not double
. Since 0x0
is nil
, and 0x0 == 0.0
:), if you try to NSLog(@"%@", 0.0);
, you get "(null)" for output. Also, if you do get a valid return from doubleValue
, your attempt to use %@
will very likely result in a crash**, because you'll be trying to touch memory you're not supposed to.
**Credit to Caleb (see comment) for pointing this out.
Upvotes: 6
Reputation: 8487
As far as I know %@
is used only for objects, and I have had nothing but problems when I have accidentally used it on primitive datatypes. I just tried it on a double, and it crashed for me. Instead, I use (for doubles):
NSLog(@"%lf", barHeight);
A comprehensive list can be found here:
Upvotes: 1
Reputation: 124997
NSLog(@"%@ %@ %@",[NSString stringWithFormat:@"%d", j], [array objectAtIndex:j], biggestTotal);
You don't need the [NSString stringWithFormat...]
bit... NSLog does that for you. Do this instead:
NSLog(@"%@ %f", [array objectAtIndex:j], biggestTotal);
Also, notice that I used %f instead of %@ as the second format specifier. This is because biggestTotal is a double, not an object pointer. The %@ specifier is for objects only. You'll want to make that same change in your second NSLog() statement.
Edit: I see now that you say that biggestTotal is a string, in which case %@ is correct. However, the second NSLog() specifies %@ for barHeight, which is a double, so you'll still need to fix that. Your first log statement is printing (null) for biggestTotal, so I suspect you have some problem wherever you create biggestTotal.
Upvotes: 1