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

Constants (static const)

This lesson covers declaring constants with static const in Objective-C, with the aim of handling values that must not change safely.

static const creates a constant whose value cannot be changed later. The static also limits its scope to the file it is in.

The sample code defines the maximum score as static const NSInteger MaxScore = 100 and uses it in a message. Unlike C's #define, it is a typed and therefore safer constant.

A common early stumble is the difference from #define. Because static const carries a type, you get the benefit of the compiler's type checking.

In professional work, constants are a basic technique for avoiding magic numbers and conveying intent.

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