2010-04-21 114 views

回答

37

是的,使用NSArray -valueForKey:方法。

NSArray *extracted = [sourceArray valueForKey:@"a key"]; 
+3

+1你每天學習的東西! – 2010-04-21 05:07:13

+0

非常感謝! – RunLoop 2010-04-21 05:07:32

+0

有時舊的答案可以拯救我的生命。非常感謝好友。 – 2015-06-03 10:38:28

10

是,只需使用鍵 - 值編碼要求爲重點的值:

NSArray* names = [NSArray arrayWithObjects: 
        [NSDictionary dictionaryWithObjectsAndKeys: 
        @"Joe",@"firstname", 
        @"Bloggs",@"surname", 
        nil], 
        [NSDictionary dictionaryWithObjectsAndKeys: 
        @"Simon",@"firstname", 
        @"Templar",@"surname", 
        nil], 
        [NSDictionary dictionaryWithObjectsAndKeys: 
        @"Amelia",@"firstname", 
        @"Pond",@"surname", 
        nil], 
        nil]; 

//use KVC to get the names 
NSArray* firstNames = [names valueForKey:@"firstname"]; 

NSLog(@"first names: %@",firstNames); 
相關問題