Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an NSMutableArray of floating point values

I'm new to Objective-C and iPhone development, and I'm trying to store floating-point values in an NSMutableArray, but when I do I get an error saying "incompatible type for argument 1 of 'addObject". What am I doing wrong? I'm trying to create an array of doubles that I can perform math calculations with.

like image 739
Jon Avatar asked Jan 29 '09 22:01

Jon


People also ask

How do you declare a floating point array?

You can define the array without the size. but it should be in this way: float array[] = {3.544, 5.544, 6.544, 6.544}; see the following topic for more details: How to initialize all members of an array to the same value?

What is NSMutableArray?

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray . NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, CFMutableArray .

How do you declare a float array in C++?

A typical declaration for an array in C++ is: type name [elements]; where type is a valid type (such as int , float ...), name is a valid identifier and the elements field (which is always enclosed in square brackets [] ), specifies the length of the array in terms of the number of elements. int foo [5];


1 Answers

NSMutableArray only holds objects, so you want an array to be loaded with NSNumber objects. Create each NSNumber to hold your double then add it to your array. Perhaps something like this.

NSMutableArray *array = [[NSMutableArray alloc] init]; NSNumber *num = [NSNumber numberWithFloat:10.0f]; [array addObject:num]; 

Repeat as needed.

like image 101
Ryan Townshend Avatar answered Oct 08 '22 11:10

Ryan Townshend