Introductory guidelines for writing your own classes

This post is a summary of the key things you need to know to begin writing your own Cocoa classes in Objective-C.


You have just had a thorough introduction to Objective-C and Cocoa.

In this section, we’ll present guidelines for writing your own classes, based on a summary of the highlights in the introductory material. The goal is to enable you to begin coding right away, and have some success.

There’s nothing new in this post, it’s just a convenient summary of the introductions to Objective-C and Cocoa.


Introductory guidelines

Know your development toolset – Xcode for edit, compile and debug; IB for building a user interface; and the iOS Simulator for running and testing your app.

Remember that you are extending existing classes and writing new classes that will respond to events in the Cocoa runtime. The Cocoa runtime is in control, and you are essentially just writing a bunch of event handlers.

Objective-C is a header-based language:

  • Declarations are written in a .h interface source code file
  • Implementations are written in a .m implementation source code file


The .h interface includes the following:

  • An #import statement for UIKit.h (iOS apps)
  • @interface and @end keywords
  • Superclass declaration, and optional protocol declaration
  • Instance variable declarations, placed in the @interface statement’s curly brace block
  • Property declarations, for the instance variables you want publicly exposed as properties
  • Method declarations, for new custom methods in your class
  • Optional initializers that customize the superclass init method(s)


The .m implementation includes the following:

  • An #import statement for this class’ .h interface
  • #import statements for any other required classes
  • @implementation and @end keywords
  • Property accessor creation statement(s) (e.g. @synthesize…)
  • Implementations of YOUR custom methods
  • Implementations of custom initializers
  • Implementations of overrides of desired superclass methods
  • Application lifecycle method implementations, including – dealloc for memory management


When using instance variables or properties, you follow three steps:

  1. Declare (in the .h interface)
  2. Initialize (in the .m implementation)
  3. Memory manage – clean up (in the .m implementation’s – dealloc method)


Naming conventions are important. Begin an instance variable name with a lowercase letter. If the name is composed of multiple words, capitalize the first letter of each word after the first word.

If you want to expose an instance variable as a property, steps 1 and 2 above are done differently:

  1. Declare… Do not double-declare – declare only as a property
  2. Initialize… Create the accessors (usually with @synthesize…), and initialize the property’s value
  3. Memory manage… (same as above)


Your new custom methods can be instance ( – ) or class ( + ) methods.

If you create initializers, there are some considerations and coding tasks to perform. In the coming weeks, we will learn more about this topic.

If you override a superclass method, you do NOT have to include its method declaration in your .h interface. (Why? It’s declared in the superclass’ .h interface.) Simply implement it in your .m implementation.

When writing method implementations, pay attention to the Objective-C syntax that is new to you. A few highlights:

  • Method names can be very different that what you’re accustomed to in C++/Java/C# – pay attention to the return type value, readable method names, capitalization, and arguments (and their types)
  • Calling a method requires square-bracket syntax: [ receiverName methodName:argument1 withInfo:argument2 ];
  • Be careful with type matching and type conversions


Do I have any other tips? Yes.

  • Pay attention.
  • Be mentally agile.
  • Learn to use the Apple developer documentation.
  • Read lots of code.
  • Write lots of code.
  • Have fun.
  • Build great software.


  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: