Tag Archives: objc-runtime

Dynamic Typing

Dynamic typing enables the runtime to determine the type of an object at runtime.

For example say we have this code:

This is a statically typed pointer. It declares that the variable myAtom is a pointer to an object of type Atom.

With static typing, the compiler performs type checking, and thus detects type errors.

With dynamic typing, type checking is performed at runtime. It is done via the id data type.

The id type is a unique Objective C type that can hold a pointer to any type of Objective C objects.

Therefore, the variable myAtom can point to any NSObject.

  • Dynamic typing permits associations between objects to be determined at runtime, rather than forcing them to be encoded in a static design.
  • Dynamic typing also gives you much greater flexibility because the types used by a program can evolve during its execution and new types may be introduced without the need to recompile or redeploy.

For example, the method can take any object:

We can also specify that whatever object we pass in must conform to protocol NSDecimalNumberBehaviors

Or, we can specify that the object type must be NSNumber

Or, that the type must be type NSNumber which conforms to protocol NSDecimalNumberBehaviors