[英]From NSArray of NSDictionary objects, how to get an NSArray of uniques
[英]How to get count of objects in NSArray inside of NSDictionary
我需要對NSDictionary內部數組中的對象進行計數。 例如:
po _dictionary
{
keys = (
"one",
"two"
);
}
考慮到這是一個數組: [_dictionary objectForKey:@"keys"]
我的問題是如何獲取數組中的計數?
我嘗試這樣:
[[[_tutorials objectForKey:@"keys"] allKeys ]count];
但我收到此錯誤:
*由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'-[__ NSCFArray allKeys]:無法識別的選擇器發送到實例0xa419ea0'
[_dictionary objectForKey:@"keys"]
是一個NSArray
而不是NSDictionary
。 因此,它不了解allKeys
方法。 刪除它,它應該可以工作:
[[_dictionary objectForKey:@"keys"] count];
NSLog(@"%d", [[_dictionary objectForKey:@"keys"] count]);
由於您的[_dictionary objectForKey:@"keys"]
是一個數組,因此您可以直接獲取計數。
干得好:
NSLog(@"%lu", _dictionary[@"keys"].count);
NSDictionary
有一種獲取NSArray
鍵的方法,我發現它比使用objectForKey:
更具可讀性objectForKey:
[[_dictionary allKeys] count]
NSDictionary *res=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error1];
NSLog(@"%d",[[res valueForKey:@"result"] count]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.