更好的方式将NSArray的NSNumbers转换为NSStrings数组 [英] Better way to convert NSArray of NSNumbers to array of NSStrings
问题描述
我有一个 NSArray
由 NSNumbers
组成,我想将其转换为通过获取每个
。 NSNumber的
stringValue
,获得 NSStrings
的NSArray
I have an NSArray
consisting of NSNumbers
and I want to convert this to an NSArray
of NSStrings
, by getting the stringValue
of each NSNumber
in the first array.
我想到的方法是迭代第一个值中的每个值,获取其字符串值并将其添加到另一个数组中。但是应该有一个更优雅的解决方案。你知道吗?
The method that comes to my mind is iterating each value in the first one, getting its string value and adding it into another array. But there should be a more elegant solution for this. Do you know one?
推荐答案
NSArray
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSKeyValueCoding_Protocol/Reference/Reference.html%23//apple_ref/occ/cat/NSKeyValueCoding\">关键字编码方法 valueForKey:
,以便返回一个新数组。新数组包含查询原始数组中每个对象的指定值的结果。在这种情况下, NSNumber
有它的 stringValue
,所以你需要做的是:
NSArray
implements the Key-Value Coding method valueForKey:
in such a way that it returns a new array. The new array contains the results of asking each object in the original array for the specified value. In this case, NSNumber
has its stringValue
, so all you have to do is:
NSArray * b = [a valueForKey:@"stringValue"];
普通旧的快速枚举(或 enumerateObjectsUsingBlock:
)不会是一个可怕的解决方案,但。 NSArray
的实施 valueForKey:
最有可能使用 for
循环内部,这将是很容易被任何人谁读它后来很容易理解。
Plain old fast enumeration (or enumerateObjectsUsingBlock:
) wouldn't be a terrible solution, though. NSArray
's implementation of valueForKey:
most likely uses a for
loop internally, and that would be pretty readily understood by anyone who reads it later.
这篇关于更好的方式将NSArray的NSNumbers转换为NSStrings数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!