2010-04-06 15 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]; 
} 

Posso scrivere il codice come sopra. Devo memorizzare un valore float (array[][]) in NSArray. Posso farlo ?
Il mio problema è, ho una matrice comeCome posso memorizzare un valore float in 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 

ho bisogno di recuperare i valori (0,4, 0,3, 0,5) con 1.0, 2.0 3.0. Come posso usare NSDictionary per questo?
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]; 
} 

Posso scrivere in questo modo? L'obiettivo C lo accetta?

sto ottenendo un errore come

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

Posso chiedere perché si desidera memorizzare una matrice 3D in NSArray? – Prcela

risposta

18

NSArray possono solo oggetti negozio, non primitive, fortunatamente, la classe NSNumber ha un metodo convenienza che prende una primitiva mobile e restituisce un oggetto float come tale:

+ (NSNumber *)numberWithFloat:(float)value 

quindi si potrebbe compilare un array come questo:

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 

Per quanto riguarda il problema specifico, si potrebbe scrivere:

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

quanto usando un dizionario per retrive valori della matrice, l'unico modo posso pensare fuori sarebbe quello di codice chiave i vostri valori di matrice in quanto tali:

A1 A2 A3 A4

B1 B2 B3 B4

C1 C2 C3 C4

D1 D2 D3 D4

ad esempio:

NSMutableDictionary *myDictionary; 

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

... 

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

Inoltre, è importante sottolineare qui che ogni volta che qualcosa è scritto sotto il format @ "something here", letteralmente è un oggetto NSString. in modo che quando si scrive:

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

questo è esattamente lo stesso di scrittura:

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

Grazie mille per avermi dato chiarimenti su NSArray e NSDictionary. Questo mi aiuta molto. –

0

Prova:

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