fuzzygoat
fuzzygoat

Reputation: 26223

Initialize NSArray with floats?

Is this a valid way to initalize an NSArray with float objects?

NSArray *fatArray = [NSArray arrayWithObjects:
                    [NSNumber numberWithFloat:6.9],
                    [NSNumber numberWithFloat:4.7],
                    [NSNumber numberWithFloat:6.6],
                    [NSNumber numberWithFloat:6.9],nil];

It works and it feels right, just wanted to make sure I was on the right track.

gary

Upvotes: 19

Views: 27779

Answers (7)

zoul
zoul

Reputation: 104065

As mouviciel already wrote, this is the way to do it. When I write something like this I usually make the code shorter using a simple macro:

#define FBOX(x) [NSNumber numberWithFloat:x]

Then you can rewrite the code like this:

NSArray *fatArray = [NSArray arrayWithObjects:
    FBOX(6.9), FBOX(4.7), FBOX(6.6), FBOX(6.9), nil];

Macros are evil, but in this case the macro is so simple I’d use it. Plus the code hurts a bit less to read, especially if the macro definition is not far.

If you wrote a lot code like this, you could create a category with a custom initializer with variable number of float arguments, but then there’s a problem ending the argument list. You could pass the total number of floats first:

- (id) initWithFloats: (int) numFloats data: (float) float1, ...;

But counting the arguments by hand is prone to error. Or you could use some sentinel value such as zero that would mark the end of the argument list, but this opens a whole new can of worms called floating-point comparison.


Please note that nowadays you can simply write the following:

NSArray *list = @[@6.9, @4.7, @6.6, @6.9];

It’s not a syntax dream come true, but it’s officially supported by the compiler and it’s much better than the previous solutions. See the documentation for more goodness.

Upvotes: 32

isaac
isaac

Reputation: 631

Another lazy option is to just use an array of NSStrings and then getting the type of value you are looking for directly from the string.

Like so:

NSArray *arrayOfNumbers = [NSArray arrayWithObjects:@"3.12",@"3.2", @"12", @"15", nil];
float number = [[arrayOfNumbers objectAtIndex:0] floatValue];

Upvotes: 1

user206499
user206499

Reputation: 11

If you have a lot of floats to convert, or want to paste them in from a comma separated list, it may be easier to call a function that converts floats into an NSArray instance.

NSArray*  arrayFromFloats(NSUInteger count, float *floats)
{
    NSMutableArray* ma = [NSMutableArray arrayWithCapacity:count]; 
    NSUInteger i; 
    for (i=0; i<count; i++) { 
        [ma addObject:[NSNumber numberWithFloat:floats[i]]];
    }
    return (NSArray*)ma; 
}

Example caller:

static float floats[] = {1.1, 2.2, 3.3}; 
NSUInteger count = sizeof(floats)/sizeof(float);
NSArray* a = arrayFromFloats(count, floats); 

Upvotes: 1

Mark Bessey
Mark Bessey

Reputation: 19782

If you're going to be creating a lot of these arrays with exactly the same number of elements, you could define a FloatArrayFactory class that implements something like:

+ (NSArray *) arrayWithFloat: (float)f;
+ (NSArray *) arrayWithFloat: (float)f1 and: (float)f2;
+ (NSArray *) arrayWithFloat: (float)f1 and: (float)f2 and: (float)f3;
+ (NSArray *) arrayWithFloat: (float)f1 and: (float)f2 and: (float)f3 and: (float)f4;

And if that's too verbose, you could go for the terse and funky look of unnamed parameters:

+ (NSArray *) arr: (float)f1 : (float)f2 : (float)f3;

which you can call like this:

NSArray *a = [Factory arr: 1.0 :2.0 :3.0 :4.0];

Upvotes: 1

pxl
pxl

Reputation: 1297

going off on a slight tangent, if all you want to do is store an array of floats and don't really need the added functionality of an NSArray or NSNumber, then you should also consider just a standard C array:

float fatArray[] = {6.6, 6.9, 4.7, 6.9};

Upvotes: 2

TalkingCode
TalkingCode

Reputation: 13557

I agree with mouviciel. Since you can't put simple types into an Array this is the way to do it. Looks a bit strange and is a lot of code to write but it is fine.

Upvotes: 1

mouviciel
mouviciel

Reputation: 67829

As far as I know, this is a perfectly valid way of putting floats in an NSArray.

Upvotes: 4

Related Questions