Array of doubles objective c

10,525

Solution 1

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]];

Solution 2

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];
Share:
10,525
Jeeter
Author by

Jeeter

Embedded Systems Software Engineer at Apple's Technology Development Group. Specialize in CPP, C, ARM systems, bare-metal drivers, with peripheral experience all around the stack from app development to machine learning to web systems.

Updated on June 04, 2022

Comments

  • Jeeter
    Jeeter almost 2 years

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

  • Jeeter
    Jeeter about 12 years
    That's what I thought. I just didn't know how to do it. Thank you!