2012-01-06 27 views

回答

6

有一個更短的形式則本上的NSArray提出休伯特

NSArray *allNames = [array valueForKey:@"name"]; 

valueForKey:通過發送valueForKey:givenKey所有它的元素返回一個新的陣列。

docs

valueForKey:
返回包含使用上的每個陣列的對象的調用key valueForKey:的結果的數組。

- (id)valueForKey:(NSString *)key

參數
key檢索的關鍵。

返回值
檢索到的密鑰的值。

討論
返回的數組包含該返回nil每個對象NSNull元件。

例子:

NSArray *array = @[@{ @"active": @NO,@"name": @"Alice"}, 
        @{ @"active": @NO,@"name": @"Bob"}]; 

NSLog(@"%@\n%@", array, [array valueForKey:@"name"]); 

結果:

(
     { 
     active = 0; 
     name = Alice; 
    }, 
     { 
     active = 0; 
     name = Bob; 
    } 
) 
(
    Alice, 
    Bob 
) 
3

這是字典對象的數組,所以讓你將這些值:

[[myArray objectAtIndex:0]valueForKey:@"name"]; //Replace index with the index you want and/or the key. 
+0

我假設你的意思是valueForKey:@「@ name」,但我試過這個,它引發NSUnknownKeyException。我從一個nsdictionary創建了這個數組,所以它可能不會被設置爲「字典對象數組」,我認爲我沒有其他選擇。參見[這裏](http://stackoverflow.com/questions/8751198/complex-nsdictionary-taken-objectsforkeys-and-sorting-them-as-nsarray-or-separa) – davis 2012-01-06 02:56:14

+0

我理解了它,並將它設置爲NSDictionary而不是NSArray。謝謝 – davis 2012-01-06 03:14:55

+0

甚至有一個更短的形式,請參閱我的答案。 – vikingosegundo 2012-01-06 03:20:58

1

是的,你可以

看看這個例子:

NSDictionary *responseDictionary = [[request responseString] JSONValue]; 
NSMutableArray *dict = [responseDictionary objectForKey:@"data"]; 
NSDictionary *entry = [dict objectAtIndex:0]; 
NSString *num = [entry objectForKey:@"num"]; 
NSString *name = [entry objectForKey:@"name"]; 
NSString *score = [entry objectForKey:@"score"]; 

我很抱歉,如果我不能細說多,因爲我也是東西

工作,但我希望能幫助你。 :)

+0

@Davis G. - 我想休伯特的答案簡單易用。 – NoobMe 2012-01-06 02:44:55

+0

@vikingosegundo - 先生您好..你能幫助我在cocos2d中我有問題,我的問題禁令這就是爲什麼我不能上傳的問題,我真的需要幫助,你能嗎? – NoobMe 2012-01-06 03:59:06

+0

什麼是禁令問題?不,我沒有使用cocos2d的經驗。 – vikingosegundo 2012-01-08 00:35:20

0

不,大家....問題是你正在踩可可中的KeyValue機制。

KeyValueCoding指定該@count符號可以在一個的keyPath使用....

myArray的。@計數

SOOOOOO ....只需切換到ObjectForKey,你就OK了!

NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"theValue", @"@name", nil]; 

id kvoReturnedObject = [myDictionary valueForKey:@"@name"]; //WON'T WORK, the @ symbol is special in the valueForKey 
id dictionaryReturnedObject = [myDictionary objectForKey:@"@name"]; 

NSLog(@"object = %@", dictionaryReturnedObject); 
3

如果你想的NSMutableArray轉換爲相應的NSDictionary,只是簡單地使用mutableCopy

NSMutableArray *phone_list; //your Array 
NSDictionary *dictionary = [[NSDictionary alloc] init]; 
dictionary = [phone_list mutableCopy]; 
+0

最好的解決方案迄今 – 2016-01-27 13:51:18

+0

這...不起作用。你的'字典'變量實際上是一個數組。 – pkamb 2016-01-27 18:32:31

0

這是例如exmple之一獲得emplyee列表NSMutableArray中創造的NSMutableDictionary ......

NSMutableArray *emloyees = [[NSMutableArray alloc]initWithObjects:@"saman",@"Ruchira",@"Rukshan",@"ishan",@"Harsha",@"Ghihan",@"Lakmali",@"Dasuni", nil]; 

NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 
for (NSString *word in emloyees) { 
    NSString *firstLetter = [[word substringToIndex:1] uppercaseString]; 
    letterList = [dict objectForKey:firstLetter]; 

    if (!letterList) { 
     letterList = [NSMutableArray array]; 
     [dict setObject:letterList forKey:firstLetter]; 
    } 
    [letterList addObject:word]; 
} NSLog(@"dic %@",dict);