要按順序顯示NSMutableDictionary中的鍵/值(在tableview中),我需要通過索引訪問它們。如果通過索引訪問可以在該索引處給出關鍵字,那麼我可以獲得該值。有沒有辦法做到這一點或不同的技術?按索引訪問NSMutableDictionary中的對象
回答
您可以使用包含對象的鍵的NSArray allKeys方法。然後你可以通過索引來查看。請注意,鍵在數組中顯示的順序未知。例如:
NSMutableDictionary *dict;
/* Create the dictionary. */
NSArray *keys = [dict allKeys];
id aKey = [keys objectAtIndex:0];
id anObject = [dict objectForKey:aKey];
編輯:其實,如果我理解你想你想很容易利用快速列舉完成,例如什麼什麼:
NSMutableDictionary *dict;
/* Put stuff in dictionary. */
for (id key in dict) {
id anObject = [dict objectForKey:key];
/* Do something with anObject. */
}
編輯:固定錯字所指出的馬可。
「allkeys」是「allKeys」 – Mark 2011-04-09 22:25:19
在tableview中的'cellForRowAtIndexPath'中可能是:'NSArray * keys = [colors allKeys]; NSString * key = [keys objectAtIndex:indexPath.row]; cell = [tv dequeueReusableCellWithIdentifier:@「CellColour」forIndexPath:indexPath]; cell.textLabel.text = [colors objectForKey:key]; cell.backgroundColor = [self colorFromHexString:key];' – 2013-11-08 14:23:33
你可以得到所有字典的allKeys
方法鍵的數組;然後你可以通過索引訪問數組。然而,字典本身並不具有固有的順序,所以對字典進行更改前後得到的鍵的順序可以完全不同
- 1. 按索引訪問JavaScript對象元素
- 2. 訪問/訪問MVC中的IEnumerable對象中的特定索引
- 3. jQuery的訪問對象通過索引
- 4. 按對象排序NSMutableDictionary鍵?
- 5. 在NSMutableDictionary中刪除索引20後的所有對象?
- 6. 按索引訪問屬性
- 7. 對象的NSMutableDictionary
- 8. 按索引問題的Django Queryset訪問
- 9. NSMutableArray訪問NSMutableDictionary
- 10. 對象中的NSMutableDictionary和NSMutableArray對象
- 11. 訪問ReferenceChange對象引用
- 12. C++對象引用/訪問
- 13. 訪問範圍變量索引在Thymeleaf中的地圖對象
- 14. 如何通過jQuery中的索引訪問對象鍵?
- 15. 如何循環訪問stdClass對象中的編號索引?
- 16. 如何訪問C#中IEnumerable對象的索引?
- 17. 訪問一個對象鍵和索引從for循環中的對象
- 18. 將對象添加到NSMutableDictionary的問題
- 19. 跨類訪問NSMutableDictionary
- 20. 按索引排列的對象
- 21. 當從NSMutableArray更改爲NSMutableDictionary時訪問對象
- 22. 如何訪問NSMutableDictionary的鍵?
- 23. NSMutableDictionary丟失對象
- 24. 按照編號/索引訪問pictureBox
- 25. 按鍵和索引訪問JSON
- 26. 紅黑樹訪問按順序索引
- 27. 按索引jsonparser訪問字段
- 28. 按內容訪問QVector元素索引
- 29. Python按值訪問元組索引
- 30. MSBuild:比較ItemGroups或按索引訪問
什麼時候做:NSMutableDictionary * tmpDict = [[NSMutableDictionary alloc] init]; [tmpDict setObject:@「CN-GSM」forKey:@「Provider:」]; [tmpDict setObject:@「10010」forKey:@「Number:」]; [tmpDict setObject:@「Yes」forKey:@「Free:」]; NSLog(@「%@」,[tmpdict allKeys]); [tmpDict發佈];爲什麼輸出不按我添加到tmpDict的值排序? – ZYiOS 2010-12-02 05:46:28