Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of doubles objective c

Is there something like NSMutableArray that will take doubles directly without putting them inside the @""?

like image 761
Jeeter Avatar asked Apr 19 '12 14:04

Jeeter


2 Answers

You can only put objects into an NSMutableArray. But you can wrap your doubles in NSNumber like so:

NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
[array addObject:[NSNumber numberWithDouble:0.12345]];
[array addObject:[NSNumber numberWithDouble:5.43210]];
like image 139
mattjgalloway Avatar answered Oct 18 '22 11:10

mattjgalloway


You can only insert objects into an NSMutableArray. Luckily, there is a class, NSNumber, that is used to wrap Objective-C primitives as objects. You can use the doubleValue method to get your primitive value back.

For example:

NSMutableArray *numArray = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithDouble:1.1f], nil];
double num = [[numArray objectAtIndex:0]doubleValue];
like image 34
Paul O. Avatar answered Oct 18 '22 09:10

Paul O.