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

Searching an array (containsObject:)

This lesson covers searching an array with containsObject in Objective-C, with the aim of checking whether a value is present.

containsObject: tells you whether a value is in the array. It is the simple search method NSArray provides as standard.

The sample code checks whether "orange" is in the array with [fruits containsObject:@"orange"]. Note the %d used to display a BOOL result.

A common early stumble is that the search takes longer as the data grows. With a lot of data, searching an NSSet is faster.

In professional work, this search comes up whenever you check whether a list contains a particular product ID or user name.

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