Member Avatar for SamD34

Hi,

I want to declare a float[] in .h and initialize in .m as explained below:
.h file

`

@interface A: NSObject
  {
    float data[10];
  }
  @end

`

.m file

@implementation A
-(void) loadData{
data={1,2,3,4,5,6,7,8,9,10};//Does not work
}
@end

I could have intialized in the following manner, but Could it be intialized in the above manner.
data[0]=1.0,.....data[9]=10.0

You can use the arrayWithObjects method:

data = [NSArray arrayWithObjects: your list of stuff, nil];

You need to include the nil, I can't tell you why...

You need the nil to indicate the end of the list. Also float is a primitive data type and so wont work in this context. You should use NSNumber (which is an object) rather than float.

[NSArray arrayWithObjects:] takes id object as argument, so one must initialize individual object with [NSNumber numberWithFloat:] method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.