NSArray 等效于 Map [英] NSArray Equivalent of Map
本文介绍了NSArray 等效于 Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定 NSDictionary
对象的 NSArray
(包含类似的对象和键)是否可以将执行映射到指定键的数组?例如,在 Ruby 中,它可以通过:
Given an NSArray
of NSDictionary
objects (containing similar objects and keys) is it possible to write perform a map to an array of specified key? For example, in Ruby it can be done with:
array.map(&:name)
推荐答案
更新:如果您使用的是 Swift,请参阅 地图.
Update: If you're using Swift, see map.
BlocksKit 是一个选项:
NSArray *new = [stringArray bk_map:^id(NSString *obj) {
return [obj stringByAppendingString:@".png"];
}];
<小时>
Underscore 是另一种选择.有一个 map
函数,这里是一个来自网站的例子:
Underscore is another option. There is a map
function, here is an example from the website:
NSArray *tweets = Underscore.array(results)
// Let's make sure that we only operate on NSDictionaries, you never
// know with these APIs ;-)
.filter(Underscore.isDictionary)
// Remove all tweets that are in English
.reject(^BOOL (NSDictionary *tweet) {
return [tweet[@"iso_language_code"] isEqualToString:@"en"];
})
// Create a simple string representation for every tweet
.map(^NSString *(NSDictionary *tweet) {
NSString *name = tweet[@"from_user_name"];
NSString *text = tweet[@"text"];
return [NSString stringWithFormat:@"%@: %@", name, text];
})
.unwrap;
这篇关于NSArray 等效于 Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文