Better way to convert NSArray of NSNumbers to array of NSStrings Better way to convert NSArray of NSNumbers to array of NSStrings objective-c objective-c

Better way to convert NSArray of NSNumbers to array of NSStrings


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.