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

Returning several values from a function (NSDictionary)

This lesson covers returning several values from one function with NSDictionary in Objective-C, with the aim of writing functions not limited to a single return value.

NSDictionary lets a function return several values as one bundle. It is a light-touch way to hand back related results together, such as a minimum and a maximum.

In the sample code, minMax() returns both in the dictionary literal @{@"min": @(minVal), @"max": @(maxVal)}. Note also the neat way valueForKeyPath: finds the minimum and maximum.

A common early stumble is the design of using strings as dictionary keys. There is no tuple as in other languages, so a dictionary with meaningful key names is the idiomatic Objective-C substitute.

In professional work, using an NSDictionary where another language would use a tuple is the usual practice.

๐Ÿ“– 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)