Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I store a float value in an NSArray?

for ( tempI = 0; tempI < 10; tempI++ )  
{
    tempJ = 1;
    NSArray *objectsForArray = [NSArray arrayWithObjects:@"array[tempI][tempJ]", @"array[tempI][tempJ+1]",  @"array[tempI][tempJ+2]", nil];
}

Can I write the code as above. I need to store an float value (array[][]) in NSArray. Can I do it ?
My problem is, I have a matrix as

1.0   0.4   0.3   0.5  
2.0   0.4   0.5   0.5  
3.0   4.3   4.9   0.5  

I need to retrieve values (0.4, 0.3, 0.5) using 1.0, 2.0 3.0 . How can I use NSDictionary for this?
Thank You

for( tempI = 0; tempI < 5; tempI++)
{
        NSDictionary *dictionary[tempI] = [ [NSDictionary alloc] initWithObjects:@"array[tempI][tempI + 1]", @"array[tempI][tempI + 2]", @"array[tempI][tempI + 3]", @"array[tempI][tempI + 4]", @"array[tempI][tempI + 5]", nil];
}

Can I write in this way ? Does Objective C accepts it ?

I am getting an error as

error: variable-sized object may not be initialized
like image 303
srikanth rongali Avatar asked Apr 06 '10 14:04

srikanth rongali


People also ask

Can NSArray contain nil?

arrays can't contain nil. There is a special object, NSNull ( [NSNull null] ), that serves as a placeholder for nil.

What is difference between NSArray and NSMutableArray?

The primary difference between NSArray and NSMutableArray is that a mutable array can be changed/modified after it has been allocated and initialized, whereas an immutable array, NSArray , cannot.

How do you declare NSArray in Objective C?

Creating NSArray Objects Using Array Literals In addition to the provided initializers, such as initWithObjects: , you can create an NSArray object using an array literal. In Objective-C, the compiler generates code that makes an underlying call to the init(objects:count:) method.

Is NSArray ordered?

The answer is yes, the order of the elements of an array will be maintained - because an array is an ordered collection of items, just like a string is an ordered sequence of characters...


1 Answers

NSArray can only store objects, not primitives, fortunatly, the NSNumber class has a convenience method that takes a float primitive and returns a float object as such:

+ (NSNumber *)numberWithFloat:(float)value

therefore you could populate your array like this:

float exampleFloat = 5.4;

NSArray *anArrayOfFloatObjects = [NSArray arrayWithObjects:
                                    [NSNumber numberWithFloat:10.0],
                                    [NSNumber numberWithFloat:2],
                                    [NSNumber numberWithFloat:4],
                                    [NSNumber numberWithFloat:exampleFloat],
                                    nil]; // Don't forget the nil to signal
                                          // end of the array

As for your specific issue, you could write:

NSMutableArray *tmpArray; // this is necessary since an NSArray can only be initialized 
                          // once and we will need to have all the objects that will be 
                          // added to the array available to us at once.

tmpArray = [NSMutableArray arrayWithCapacity:12]; // returns an autoreleased empty array


for (int col=0; col<=3; col++) {
    for (int row=0; row<=2; row++) {
        [tmpArray addObject:[NSNumber numberWithFloat:array[col][row]]];
    }
}
NSArray *myArray = [NSArray arrayWithArray:tmpArray];

as far as using a dictionary to retrive matrix values, the only way I can think off would be to key code your matrix values as such:

A1 A2 A3 A4

B1 B2 B3 B4

C1 C2 C3 C4

D1 D2 D3 D4

for example:

 NSMutableDictionary *myDictionary;

[myDictionary setObject:[NSNumber numberWithFloat:5.0] forKey:@"A1"];

...

NSNumber *myFloat = [myDictionary objectForKey:@"A1"];

Also, it is important to point here that whenever something is written under the format @"something here", it literally is an NSString object. so when you write:

NSArray *objectsForArray = [NSArray arrayWithObjects:
                                             @"array[tempI][tempJ]",
                                             @"array[tempI][tempJ+1]",                                                                                                                                            
                                             @"array[tempI][tempJ+2]",
                                             nil];

this is exactly the same as writting:

NSString *newString = @"Roses are red";    // example strings
NSString *newString1 = @"Violets are blue";
NSString *newString2 = @"array[tempI][tempJ+1]";
NSString *newString3 = @"These are all string objects";

NSArray *objectsForArray = [NSArray arrayWithObjects:
                                             @"array[tempI][tempJ]",
                                             newString2,                                                                                                                                          
                                             @"array[tempI][tempJ+2]",
                                             nil];
like image 146
FatalMojo Avatar answered Oct 11 '22 22:10

FatalMojo