NSMutableArray *output = [[NSMutableArray alloc]init];
NSArray *output = [[NSMutableArray alloc]init];
我可以設置鍵和值。現在,我只想訪問每個鍵和值,但我不知道設置的鍵的數量。
在PHP這是很容易的東西如下:
foreach ($output as $key => $value)
怎麼可能在Objective-C?
NSMutableArray *output = [[NSMutableArray alloc]init];
NSArray *output = [[NSMutableArray alloc]init];
我可以設置鍵和值。現在,我只想訪問每個鍵和值,但我不知道設置的鍵的數量。
在PHP這是很容易的東西如下:
foreach ($output as $key => $value)
怎麼可能在Objective-C?
我認爲你與NSArray與NSArray混淆。
在NSDictionary中你可以設置與鍵對象一樣
NSMutableDictionary *output = [[NSMutableDictionary alloc]init];
[yourDictionary setObject:@"yourvalue" forKey:@"yourKey"];
,你可以得到的物體,像
[yourDictionary objectForKey:@"yourKey"];
在你的陣列,它只是將對象的索引。
NSArray * output = [[NSArray alloc] initWithObjects:@"option1",@"option2", nil];
獲得對象
[output objectAtIndex:1]; // 1 is the index number
對於字典,您應該使用objectForKey,而不是valueForKey。後者用於鍵值編碼。 – benzado 2012-04-02 09:31:47
使用NSDictionary管理鍵和值
對於一個NSArray:
for (id item in output) {
// go nuts
}
對於一個NSDictionary:
for (id key in output) {
id value = [output objectForKey:key];
// go nuts
}
我使用的是通用的id
類型,因爲我不知道知道你正在使用什麼類型的鍵和值。如果你知道他們,你應該使用特定的類名。
在你的榜樣採取的值對象的第二陣列和使用的NSDictionary:
NSDictionary *dic = [[NSDictionary alloc] initWithObjects: objectsArray
forKeys: output];
答案可能是此http://計算器.com/questions/2300256/fast-enumeration-with-an-nsmutablearray-that-holds-an-nsdictionary? (我的意思是,它包含答案,但問題不同) – ShinTakezou 2012-04-02 09:24:36