Constants and Statics


A constant is an identifier whose value cannot be changed. It is said to be read-only. Think of it as a literal that can be referenced by name. Constants are useful when you need to reuse a literal value, for example when setting a margin for multiple UIView objects. If this margin ever needs to change, you would only need to do it once where the constant is defined instead of throughout the code.

A constant is defined using the const keyword.

Constants must be declared and defined on the same line. If you try to do this in more than one step, the compiler will throw an error, thinking that you’re trying to edit the constant.

It’s important to be able to recognize constants in your code, so you should use a different format than variables for naming them. There are many different conventions used for naming constants. One is to capitalize the name and use underscores for spacing.

Another is to prefix the name with the letter k.

Constants are widely used to store names for keys and identifiers. We can edit our notifications app from last class to do just that.

Read-only Properties

Sometimes you’ll want to make properties available to outside classes only for viewing (reading) and not for editing (writing). This can be achieved by adding the readonly modifier when declaring a property. What this does behind the scenes is only create a getter and not a setter for the property variable.


A macro is a find-and-replace rule applied to source code before it is compiled. Macros are declared using the #define keyword.

Macros look very similar to constants, and are often used for similar purposes. However, the way they are integrated into source code is very different. Constants are handled similarly to variables by the compiler. They are stored in a table in memory, and their value is read whenever their name is encountered during compilation. Macros are interpreted in the pre-compilation phase. Much like a find-and-replace operation, a script goes through the entire code and replaces any occurrences of the macro name with its value. Macros have the advantage of running faster, since their value is immediately available. Constants on the other hand, are more useful for debugging, as they can be referenced when an application runs.

Another main difference is that constants are typed, whereas macros are not.

Macro errors can also be hard to debug as the compiler will not always provide you with a relevant message. Consider the following two seemingly correct macro definitions.


One of the greatest advantages of macros is that you can use them to write small scripts. These scripts can be used like functions, as they will get inserted wherever the macro is used.

You can even pass arguments to macros if needed.

Static Attributes

A static attribute is owned by the class itself, as opposed to by an instance of that class. Its lifetime spans the entire time that the app is running, as it is not associated with any particular instance of the class.

We’ve already seen static methods before, they are the methods whose definition begins with a +. In Objective-C, these are called class methods. One of the most commonly used ones is NSString‘s stringWithFormat:.

Variables can also be static and these are used locally inside a class implementation. They are defined using the static keyword.

You can initialize static variables in the initialize class method. This method is called on every class in an app before it is used.

A static variable can be used by any instance of the class it belongs to.

If you ever want to use a static variable outside of the class its defined in, you need to create a static getter for it.

Header Source


The singleton design pattern is used when you want to restrict the instantiation of a class to a single object. Note that this is not the same as having a static class; The class does get instantiated, but only once. This is particularly useful when you need some type of coordination within the entire app. An example of this would be a data manager singleton, which ensures that the entire app is populated with the same set of data, and that any change to this data is reflected all around.

When creating a singleton, you need to insert checks to ensure that only one instance of the class is ever created. This can be done by using a static variable for the singleton, and setting it up in the initialize class method.

Header Source

As nothing prevents the rest of the program to call initialize directly, we need to add a check to make sure our singleton is only instantiated once.

We can simplify this even more, by initializing our singleton directly in the getter.

This will work fine in most situations. However, it does not take a special case into consideration. If two threads in your app try to access the singleton at the same time, sharedInstance might be called a second time before it completes its first run, and might create two instances of the singleton. Although this is unlikely, it might happen, and a safety against this condition must be created. This is also the type of bug that is nearly impossible to track down, so it’s better to be safe than sorry.

Using a @synchronized block will ensure that whatever code is inside of it will never execute more than once concurrently.

  1. Constant (programming). Wikipedia, Sep 24 2012.

  2. Macro (computer science). Wikipedia, Oct 24 2012.

  3. Singleton pattern. Wikipedia, Oct 23 2012.

Comments are closed.