2012-11-15 44 views
-6

如何獲取字典的值不是循環。我知道如何使用單個值:我如何在xcode中讀取數組?

NSString *valueStr = [dict objectForKey:@"Key2"]; 

我需要通過循環所有鍵來獲取,我需要搜索字典中的值。

+2

請更清楚你想要達到的目標。按數組你是指字典嗎?你想要取什麼? – lammert

回答

0
for (NSString *key in <#dictionary#>) 
{ 
    <#!loopcontents#> 
    [dictionary objectForKey:key] 
} 
0

你似乎有一個NSDictionary,您可以訪問所有的鍵/值:

NSArray *keys = [dict allKeys]; 
NSArray *values = [dict allValues]; 

闖民宅https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html

當然,你也可以遍歷該鍵 - 值對與

for (NSString *key in [dict allKeys]) { 

    //get value 
    NSString *value = [dict objectForKey:key]; 

    //print to log 
    NSLog (@"%@, %@", key, value); 
} 
0

結合Omarj和Rene的答案:

for (NSString *key in [dict allKeys]) 
{ 
    NSString *value = [dict objectForKey:key]; 
} 
+0

謝謝你,我有同樣的想法,並編輯我的帖子,而你回答:) – Herm

1

,因爲它看起來像你試圖用一個NSDictionary你會想要做這樣的事情:

for (NSString *key in [dict allKeys]){ 
    NSLog(@"%@", [dict objectForKey:key]); 
} 

,或者如果你不想使用循環您可以執行以下操作:

NSArray *keys = [dict allKeys]; 
NSArray *values = [dict allValues]; 
相關問題