2012-05-06 57 views
1

我有一個字典數組,只有一個詞條。只有一個條目的NSDictionary訪問

___  ___________ 
|___| --->|__|__|__|__| Array of Dictionary 
|___|  __|__ 
|___|  |K|V| Dictionary 
|___| 

Array of Array 


My dictionary content is <NSString*, UIImage*> 

如何檢索我的字典的值。

代碼段:

for (int i=0 ; i< [imagesToFill count]; i++) { 

    NSDictionary* dict = [imagesToFill objectAtIndex:i]; 

    UIImage* img = ??? [dict] // How to retrive value from above dict which has only 1 entry 
    NSString* imgPath = ??? 

} 


Note: Each dict has <NSString*, UIImage*> 

回答

2

你可以得到這樣的一本字典的所有鍵:

NSArray *allKeys = [dictionary allKeys]; 

,當然同樣是適用於所有的值:

NSArray *allValues = [dictionary allValues]; 

然後只需使用objectAtIndex:0(或lastObject)來訪問您的密鑰或值。

這兩個數組的順序沒有定義(即隨機)。這對你的情況不會有任何問題。但記住這一點。

2

如果你有鑰匙,然後就可以像特定鍵來訪問字典元素:

UIImage* img = [dict objectForKey:@"myKey"]; 

如果你不想通過密鑰才能訪問,那麼你可以轉換字典陣列並訪問其第一個元素如:

NSArray * values = [dict allValues]; 
UIImage* img = [values objectAtIndex:0];