NSPredicates 可以用来用字典中的值替换数组中的对象吗?
如果我有一个像这样的 NSDictionary:
NSMutableDictionary *valuesDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:-60.0],@”a”,
[NSNumber numberWithDouble:0.0],@”b”,
[NSNumber numberWithDouble:-12.0],@”c”,
[NSNumber numberWithDouble:.3],@”x”, nil];
和一个像这样的数组:
NSArray *program = [NSArray arrayWithObjects: @"a",@"12.6",@"100",@"+",@"x",nil];
返回一个数组 programWithVariableValues 的代码会是什么样子,例如,由 @”-60″,@”12.6″,@”100″,@”+ 组成",@".3",无?
(用它们的 valueforkey 替换数组中找到的任何变量)
这是使用 NSPredicate 的好地方吗?或者只是快速枚举?或者其他什么?
If I had an NSDictionary like this:
NSMutableDictionary *valuesDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:-60.0],@”a”,
[NSNumber numberWithDouble:0.0],@”b”,
[NSNumber numberWithDouble:-12.0],@”c”,
[NSNumber numberWithDouble:.3],@”x”, nil];
and an array like this:
NSArray *program = [NSArray arrayWithObjects: @"a",@"12.6",@"100",@"+",@"x",nil];
what would the code look like to return an array programWithVariableValues, for example, consisting of @”-60″,@”12.6″,@”100″,@"+",@”.3″,nil?
(replacing any variables found in the array with their valueforkey’s)
Would that be a good place to utilize NSPredicate? or just fast enumeration? or something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能有一种使用谓词和/或 valueForKeyPath 运算符的聪明的单行解决方案,但在有人弄清楚这一点之前,这应该可以解决问题:
programWithVariableValues 数组现在包含您的值(作为字符串)。如果您希望将数字保留为 NSNumbers,请删除“[...description]”调用。
There may be a clever one-liner solution using predicates and/or valueForKeyPath operators, but until somebody figures that one out, this should do the trick:
The programWithVariableValues array now contains your values (as strings). If you'd prefer to keep the numbers as NSNumbers, take out the "[... description]" call.