Add floatValues to NSArray add then output total
My brain is fried! I can't think.
I'm using Core Data, and this is an iPhone app just so yo开发者_C百科u know. What i am trying to do is take the floatValues (like amount paid) from text fields and them to an array, add the values in the array and output the total to a label.
For some odd reason I honestly can't think.I'm scattered brained at the moment. Please help!
Thanks in advance. -T.
The NSArray in Objective C stores only Objects, so you will need to convert the float integral value into an NSNumber object using the following code and of course back.
[arr addObject: [NSNumber numberWithFloat: fV]];
float total = 0.0;
for (NSNumber v in arr)
{
total += [v floatValue];
}
However, you could easily create a float[] c-style array and iterate with a simple for loop over it.
You can add it to the array using NSNumber
or its subclass NSDecimalNumber
. NSDecimalNumber
s can be added using decimalNumberByAdding:
too.
精彩评论