Introduction to Objective-C

  • High-level, object-oriented programming language.
  • Main programming language used by Apple for OS X and iOS.
  • Strict superset of C. It is possible to compile any C program with an Objective-C compiler, and to freely include C code within an Objective-C class.[1]

Properties

  • Instance variables are not accessed directly, they are accessed using properties.
  • A property is the combination of a getter and setter in a class:
    • The getter’s name is the name of the property:
      someValue
    • The setter’s name is the capitalized name of the property prefixed by “set”:
      setSomeValue:

Syntax

All classes inherit from NSObject.

Header Source

Here is the traditional, uncool way to create a private variable with a getter and setter:

Header Source

Here is the better way, using a synthesized property.

Header Source

This automatically creates the weight getter and the setWeight: setter.

Header Source

You can manually name a getter and/or setter, but that’s usually a bad idea as it breaks convention.

Header Source

You can make a property readonly, in which case it won’t have a setter.

Header Source

The Cat and Mouse classes extend Animal, and they inherit its public properties and methods.

Header Source

  1. Objective-C. Wikipedia, Aug 27 2012.

Comments are closed.