Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 23 / 68

Transforming an array (the map equivalent)

This lesson covers transforming each element of an array in Objective-C, with the aim of implementing the equivalent of map.

Objective-C has no standard map() method as other languages do, but a for loop that fills a new array does the same thing. Picture a machine on a conveyor belt that processes every product it passes in the same way.

The sample code uses an NSMutableArray, walks the original with for (NSNumber *n in nums), and adds the doubled value to the new array with [doubled addObject:@([n integerValue] * 2)]. Note the @() notation converting a number to an NSNumber.

A common early stumble is converting between NSNumber and NSInteger. Putting a number in an array means converting it to the object type NSNumber, which makes numeric array work in Objective-C a little more involved.

In professional work, this shape comes up whenever data is transformed in bulk — applying tax to prices, converting units.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Objective-C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)