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

我可以如上編寫代碼。我需要在NSArray中存儲浮點值(array[][])。我可以做嗎 ?
我的問題是,我有一個矩陣作爲如何在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 

我需要使用1.0,2.0 3.0〜檢索值(0.4,0.3,0.5)。我該如何使用NSDictionary?
謝謝

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

我可以這樣寫? Objective C是否接受它?

我得到一個錯誤

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

請問你爲什麼要存儲一個三維矩陣到的NSArray? – Prcela 2010-11-29 10:50:42

回答

18

NSArray中只能存儲對象,而不是原語,fortunatly的NSNumber的類有需要的便捷方法一個浮點型圖元並返回一個浮點對象,如下所示:

+ (NSNumber *)numberWithFloat:(float)value 

因此你可以填充你的陣列是這樣的:

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 

至於你的具體問題,你可以寫:

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

儘可能使用字典以檢索矩陣值,唯一的辦法我可以認爲關鍵是編碼您的矩陣值,例如:

A1 A2 A3 A4

B1 B2 B3 B4

C1 C2 C3 C4

D1 D2 D3 D4

例如:

NSMutableDictionary *myDictionary; 

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

... 

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

另外,在這裏指出,無論何時東西下的寫入是非常重要的format @「here here」,它實際上是一個NSString對象。所以,當你寫:

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

這是書面方式如出一轍:

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

非常感謝給我澄清NSArray和NSDictionary。 這可以幫助我很多。 – 2010-04-07 04:27:48

0

嘗試:

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