views:

63

answers:

3
NSArray chemConstantArray = [[NSArray alloc] initWithObjects:0.0021400, 0.0012840, 0.0010700, nil];

Gives me four errors:

Incompatible type for argument 1 of 'initWithObjects:'

Invalid initializer

Statically allocated instance of Objective-C class 'NSArray' x 2

Which makes sense, as floats are not objects, but how can I make an array of floats. I need one for BOOLs too.

A: 

You want NSNumber, which can hold floats, integers, etc.

calmh
+3  A: 

If you need an array purely within your own code, you can use a regular C array:

float chemConstantArray[] = {0.0021400, 0.0012840, 0.0010700};

If you need an NSArray* for something, you need to wrap each value into an NSNumber.

NSArray *chemConstantArray = [[NSArray alloc] initWithObjects:
    [NSNumber numberWithFloat: 0.0021400],
    [NSNumber numberWithFloat: 0.0012840],
    [NSNumber numberWithFloat: 0.0010700],
    nil];

You can use numberWithBool similarly for BOOLs.

Walter Mundt
Thanks - I used a C array like you suggested and used it by referring to `chemConstant[i]` where `i` is the index in my `for loop` building my `Chemical` objects.
Steve