Home > 2010 Winter DPS913 > Introductory guidelines for writing your own classes

Introductory guidelines for writing your own classes

January 13, 2010 Leave a comment Go to comments

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 iPhone 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 (iPhone OS 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 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, 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.

If you want to expose an instance variable as a property, two more steps are required:

  1. Declare the instance variable as a property (in the .h interface)
  2. Create the accessors (usually with @synthesize… in the .m implementation)

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.


Advertisements
Categories: 2010 Winter DPS913
  1. No comments yet.
  1. January 13, 2010 at 8:11 am

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: