简体   繁体   中英

Short Getting objects from dictionary with key to array

I got array A. It contains dictionaries with NSStrings for key "b".

I need to get all NSStrings in new Array.

I do not want to do it in more then 1 string of code. Is it possible?

I need it because I want to insert array of objects for key "b" in

  NSMutableArray *videosStr = [NSMutableArray arrayWithArray: HERE ];

Or how is the shortest way to do it? ALso I need original sorting

尝试执行此操作以获取键为"b"所有元素,

NSMutableArray *videosStr = [NSMutableArray arrayWithArray: [dictionayArray valueForKey:@"b"] ]; 

You can use Key Value Coding for it. Following is the sample code:

NSDictionary *dict1 = [NSDictionary dictionaryWithObject:@"test1" forKey:@"b"];
NSDictionary *dict2 = [NSDictionary dictionaryWithObject:@"test2" forKey:@"b"];
NSDictionary *dict3 = [NSDictionary dictionaryWithObject:@"test3" forKey:@"b"];
NSArray *arr = @[dict1, dict2, dict3];
NSArray * videosStr =[arr valueForKeyPath:@"b"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM