- 01Setting up (what you'll need)
- 02Your first output (NSLog)
- 03Using variables
- 04Branching (the if statement)
- 05Repeating (the for loop)
- 06Writing your own function
- 07Working with arrays
- 08Repeating (the while loop)
- 09Working with classes (object orientation)
- 10Error handling (@try / @catch)
- 11Sorting an array
- 12Recursive functions
- 13The string standard library
- 14The switch statement
- 15The ternary operator
- 16Extending a class with a category
- 17Writing comments
- 18Logical operators (&&, ||, !)
- 19Constants (static const)
- 20Splitting and joining strings
- 21Writing nil-safe code
- 22Searching an array (containsObject:)
- 23Transforming an array (the map equivalent)
- 24Two-dimensional arrays (tabular data)
- 25Custom errors (categorising with NSError)
- 26Writing a variadic function
- 27Handling sets (data without duplicates) with NSSet
- 28Checking correctness (a first step towards testing)
- 29Higher-order functions (passing a block as an argument)
- 30Stacks and queues (the basics of data structures)
- 31The basics of type conversion (casting)
- 32An introduction to regular expressions (pattern matching)
- 33The binary search algorithm
- 34Building a Caesar cipher (a letter-shifting cipher)
- 35Understanding how bubble sort works
- 36Assembling and displaying a date (NSDateComponents)
- 37Writing several tests together (multiple test cases)
- 38Speeding up calculation with memoization (caching)
- 39Normalizing strings (trimming whitespace, unifying case)
- 40Shallow copies versus deep copies
- 41The basics of enums
- 42Flattening an array
- 43Reversing a string and testing for a palindrome
- 44Pairing up two arrays (the zip operation)
- 45Rounding numbers (floor, ceil, round)
- 46Multi-line text (joining adjacent string literals)
- 47Returning several values from a function (NSDictionary)
- 48Finding the GCD and LCM (the Euclidean algorithm)
- 49Formatting numbers (aligning digits and decimal places)
- 50Cleaning up with @try / @catch / @finally
- 51Type-agnostic general functions (the id type)
- 52The basics of reading and writing files
- 53Generating random numbers
- 54Bitwise operations (AND, OR, XOR, shifts)
- 55Receiving command-line arguments
- 56Pausing for a set time ([NSThread sleepForTimeInterval:])
- 57Watch out for floating-point error
- 58Reading a value from standard input
- 59FizzBuzz (the classic exercise)
- 60Testing whether a number is prime
- 61Set operations with NSSet (union, intersection, difference)
- 62Converting between bases (binary and hexadecimal)
- 63Checking that brackets match (an application of stacks)
- 64Testing whether two words are anagrams
- 65Testing whether a year is a leap year
- 66Converting temperatures (Celsius and Fahrenheit)
- 67Finding the prime factors
- 68[Project] Build a simple grade-tallying program
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.
๐งช 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).
