Better way to convert NSArray of NSNumbers to array of NSStrings

后端 未结 1 1234
别跟我提以往
别跟我提以往 2020-12-10 15:02

I have an NSArray consisting of NSNumbers and I want to convert this to an NSArray of NSStrings, by getting the str

相关标签:
1条回答
  • 2020-12-10 15:28

    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"];
    

    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.

    0 讨论(0)
提交回复
热议问题