iOS Development

Course No. 10-152-139

Chapter 18 – Your First Class

Create a Class

  • First create a new Xcode project.

  • Make it a Command Line Tool.

  • Name it BMITime and make sure it is the Type Foundation. Save the project.

  • Click once on the main.m file. The new files we are about to create will be placed right after the main.m file. This is a good place for them.

  • Now we’ll make the new class. Select the File→New→File… menu item.

    • You can also just type ⌘-N.
  • Select the Cocoa→Objective-C class template.

  • Name the new class BNRPerson and make it a subclass of NSObject.

  • Xcode will ask you where you want it saved. Just take the default, that’s a fine place to put it.

  • You now have two new files in your project, BNRPerson.h and BNRPerson.m.

    • They might be in a different place in your project if you didn’t click on the main.m file first. That’s OK or you can move them where you want them to be.
  • Here’s the BNRPerson.h file.
  • And here’s the BNRPerson.m file.

More detail on a new Class.

First the BNRPerson.h file.

  • Line 9 imports the Foundation library. This includes C and the Objective-C extensions.
  • Line 11 declares a class. Wait, it uses the word interface to define a class? Yes, it does.
  • The Objective-C keyword @interface is how we start declaring a class.
  • Then we name our class.
  • The colon means that our class inherits from what comes after it.
  • NSObject is the class we are inheriting from. We’ll learn more about that soon.
  • The @end statement ends our class.

Now the BNRPerson.m file.

  • We have to import the BNRPerson.h file. Xcode did this for us.
  • Now we implement the code for this class. The @implementation statement is where we start.
  • We @end this file, too.
  • Finally, we have a place to write code.

Adding Instance Variables

  • Next, we’re going to add some variables to the BNRPerson.h file. They are called instance variables and look like this.
  • First, we add a set of curly braces.
  • Then add the instance variables. These are normal C variables. We can also have Objective-C variables. This looks a lot like creating variables in a C struct.

Accessor methods

  • Accessor methods give us access to our variables. This is one of the most important parts of Object-Orientation. We call this data hiding. We want our variables to not be accessible to code from outside of our class. In order for other code to use data from our class they have to use accessor methods.
  • Next, we declare some Accessor methods.
  • The methods that assign the values of the instance variables are on lines 18 and 21. We call this type of method a setter method.
  • The getter methods are declared on lines 17 and 20.
  • Why don’t the getter methods start with the word “get”? This is a very strong naming convention among the Objective-C community. The name of the getter methods is the same as the name of the instance variables. You will always see this in books and examples found on the internet.

The Implementation Code

  • Switch to the BNRPerson.m file to write the real code.
  • The method signatures have to be exactly the same as in the .h file so either copy/paste or let completion do its thing. Then add the curly braces and the code. These methods are pretty simple, just return the instance variable for the getters and assign the instance variables with the parameters for the setters.

The BMI Calculation Method

  • We also have a method that is not an accessor method, bodyMassIndex. The method is also declared in the declaration in the .h file.
  • With the real code in the .m file.

Use Our New Class

  • Now we’re ready to use the class. Switch to the main.m file and add this import statement.
  • Then add this code inside the @autoreleasepool.
  • First, make a new person object.
  • Then call the setter methods for the height and weight.
  • Use the get methods.
  • Calculate the BMI by sending the bodyMassIndex message.
  • Finally, output the results using the getter methods.


  • There is one part of the above that needs to be changed. This is not because of what the author of the book calls, “religious” reasons. It is good Object-Oriented practice to always use accessor methods if you have them. This line does not do that:
  • How do we send messages to other methods that are part of the same class? In other languages you can just write the name of the method.
  • Objective-C does not use the same syntax based on parentheses. It uses square brackets and you always have to have a receiver. If we are using an object outside of the class, like in our main.m file, we use the name of the pointer.
  • However, when we are inside our class we use the word self. This is a pointer to the object we are in right now. Here’s how we use a getter method from another method in our class.
  • Now we can clean up our bodyMassIndex method by using the getter methods.
  • Our full BNRPerson.m is now this.

Multiple files

  • We will all get used to using lots of files in iOS or OS X apps. There can be hundreds in a significant one.

Class prefixes

  • Objective-C does not have namespaces. Therefore it is possible to link in a library that also has a BNRPerson class.
  • To prevent name clashes (or collisions), iOS developers typically use a class prefix. The book typically uses BNR (for Big Nerd Ranch). It is recommended that you use your initials as a class prefix when creating your classes.