2010-04-06 16 views
7
for (tempI = 0; tempI < 10; tempI++) 
{ 
    tempJ = 1; 
    NSArray *objectsForArray = [NSArray arrayWithObjects:@"array[tempI][tempJ]", @"array[tempI][tempJ+1]", @"array[tempI][tempJ+2]", nil]; 
} 

¿Puedo escribir el código como el anterior? Necesito almacenar un valor flotante (array[][]) en NSArray. Puedo hacerlo ?
Mi problema es que tienen una matriz como¿Cómo puedo almacenar un valor flotante en un NSArray?

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

necesito para recuperar los valores (0,4, 0,3, 0,5), utilizando 1,0, 2,0 3,0. ¿Cómo puedo usar NSDictionary para esto?
Gracias

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]; 
} 

puedo escribir de esta manera? ¿Objective C lo acepta?

estoy recibiendo un error como

error: variable-sized object may not be initialized 
+1

¿Puedo preguntar por qué desea almacenar una matriz 3D en NSArray? – Prcela

Respuesta

18

NSArray sólo puede almacenar objetos, no primitivas, por suerte, la clase NSNumber tiene un método de conveniencia que lleva una primitiva flotante y devuelve un objeto flotante como tal:

+ (NSNumber *)numberWithFloat:(float)value 

por lo tanto, se podría llenar la matriz de esta manera:

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 

cuanto a su problema específico, se podría escribir:

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]; 

por lo que el uso de un diccionario para retrive valores de la matriz, la única manera de se me ocurren fuera sería código de la llave a sus valores de la matriz tales como:

A1 A2 A3 A4

B1 B2 B3 B4

C1 C2 C3 C4

D1 D2 D3 D4

por ejemplo:

NSMutableDictionary *myDictionary; 

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

... 

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

Además, es importante señalar aquí que siempre que algo se escribe bajo la format @ "algo aquí", literalmente es un objeto NSString. por lo que cuando se escribe:

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

esto es exactamente lo mismo que la escritura:

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]; 
+1

Muchas gracias por darme una aclaración de NSArray y NSDictionary. Eso me ayuda mucho. –

0

Probar:

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