skip to content

The Sun on Your Face

Blog Entry: Objective-C Basics for iPhone Development

Objective-C Basics for iPhone Development

Development

23rd May 2010

Overview

I have recently started to develope some of my own iPhone applications using Objective-C. I have a fairly strong background in OOP and so I am comfortable with the concepts involved. However, there were still a number of basic questions that arose that needed answering. This blog basically details those answers.

There is not really any structure to the questions - they are just the questions that I found myself asking as I built my first few applications.

What is a .h file?

A .h file is a header file. It contains the class declarations, including the instance variable declarations and the method declarations. They do not contain any implementations. They are a specification of what the classes can do, but they do not define how that is achieved.

They also include the @property statements that specify the what accessor methods are required for which instance variables.

What is a .m file?

A .m file is a source file. It contains the code to implement the methods specified in the .h files.

They also contain the @synthesize statements that tell the compiler to create the accessor methods specified in the .h file.

The #import directive is used to include the header file in the .m file.

What is the syntax for defining a class?
	 
	 @interface myClass : parentClass {
	 
		//declare instance variables
	}

	//declare methods
	
	@end
	
	

A class declaration always begins with @interface and ends with @end.

What is the syntax for defining a property?

The basic syntax is

variableType variableName;

Objective-C supports both strong and weak typing. The * is used to designate a strongly typed variable e.g.

myClass *myVariable;

declares that myVariable must be of class myClass.

A weakly typed variable is designated as type id which means it is an object of some kind. It is often used for collection classes where the exact type may not be known. A weakly typed variable is declared as

id myVariable;

What does the '*' in *propertyName signify?

The * in *propertyName specifies that the variable is strongly typed.

What is the syntax of a method declaration?

- void insertTeam: (footballTeam)aTeam atIndex: (NSUInteger) anIndex

Where:

How do we implement a class?

The implementation of a class is similar syntactically to its specification. It begins with @implementation and ends with @end.

It then must implement the methods specified in its corresponding interface.

	 
	 @implementation myClass 

	 	- (void)instanceMethod1: (parameterType) aParameterName {
			//method implementation
	 	}

	 	- (id)instanceMethod2: (parameterType) aParameterName{
		 	//method implementation
	 	}

	 	+ (void)classMethod1: (parameterType) aParameterName{
		 	//class method implementation
	 	}

	 @end

	
	

What does the @ in the @property statement signify?

It specifies that it is a declared property. A declared property is convenience notation used to replace the declaration and, optionally, implementation of accessor methods. If you want to automatically implement the accessor methods, you use the @synthesize statement in the .m file.

What is the syntax of the @property declaration?

The basic syntax just declares the type and the name of the property e.g.

@property int aNumber;
@property footballTeamClass aTeam;

Custom options further define how the accessor methods must behave e.g. you could specify that the instance variable must be Read Only

@property (readonly) UIView *rootView;

This would mean that only a getter method can be implemented.

What do the nonatomic/retain parameter values indicate?

These are custom options and determine how the accessor methods are implemented if @synthesize is used. Nonatomic/atomic refer to return values in multithreaded systems. Since iPhone applications are not multithreaded and nonatomic is quicker, it should always be set.

Objective-C uses reference counting for memory management. When an object is allocated, then the reference counter is increased by 1. Similarly, if retain is set, then this reference counter is again incremented by 1.

So if retain is set as an option for @property, a reference to any objects created will be maintained. When the object is no longer required it must be released. This is very important for iPhone applications when RAM memory is limited.

What is the purpose of @synthesize?

@synthesize is declared in the .m file and tells the compiler to automatically create the accessor methods specified by @property in the .h file.

What does the @ signify when declaring a String variable?

A character can be directly assigned using ' ' and a String can be directly assigned using " " e.g.

myChar = 'A';
myString = "Mark";

However, Objective C provides String classes that allow you to create string objects with a great deal of associated functionality e.g. the NSString class. For example

NSString *myString = [NSString stringWithFormat:@"%d %s", 1, @"String"];

This will create an object myString of class NSString using the constructor with the method stringWithFormat: . The @ notation tells the compiler to create a string object as opposed to a string literal.

Formatting Strings

In format strings, a % character announces a placeholder for a value, with the characters that follow determining the kind of value expected and how to format it. For example

%d houses expects an integer in place of the %d e.g. "6 houses"

A list of common Objective-c specifiers is

SpecifierReference
%%The '%' character
%d, %D, %iSigned 32-bit integer (int)
%u, %UUnsigned 32-bit integer (unsigned int)
%hiSigned 16-bit integer (short)
%huUnsigned 16-bit integer (unsigned short)
%f64-bit floating-point number (double)
%c16-bit Unicode character (unichar)
%sA null-terminated array of 8-bit unsigned characters. i.e. a String
%d, %D, %iSigned 32-bit integer (int)
%@Objective-C object

A full list of specifiers can be found here:

Objective-C Format Specifiers

The stringWithFormat; method above will create a string object. It takes a given format string as a template and returns a string where the other agument values are substituted for the place holders.

So in our example:

NSString *myString = [NSString stringWithFormat:@"%d %s", 1, @"String"];

@"%d %s" is the string template

The integer 1 will be substituted for %d in the new string instance

The String object "String" will be substituted for %s.

The NSString class defined in Objective-C is covered here:

Overview of the NSString Class.

What is an Outlet?

An outlet provides the reference between the user interface and the controller class in accordance with the MVC paradigm. (The UI is the View and the controller class is the Controller).

It is implemented via an instance variables in the controller class. Each appropriate widget in the UI has a corresponding instance variable of type IBOutlet defined in the .h file.

	 
		@interface FootballTeam: Team {
			UILabel *lblTeamName //declare an instance variable to hold the
								 //team name
		}
		//set the accessor methods and declare the type as an outlet
		@property (nonatomic, retain) IBOutlet UILabel *lblTeamName
		//methods
	
	

It allows the Controller class to pass information back to the UI and vice versa e.g.

What is an Action?

An action allows user interaction to be passed from the User Interface to the Controller class.

It allows users actions on the UI to invoke methods on objects of the controller class. E.g. a showAlert() method may be assigned to the touchup event of a button.

A single event can fire multiple actions and the same action can be associated with multiple events.

An action is specified in the .h file by the type IBAction.

	 
		@interface FootballTeam: Team {
			UILabel *lblTeamName //declare an instance variable to hold the
								 //team name
		}
		//set the accessor methods and declare the type as an outlet
		@property (nonatomic, retain) IBOutlet UILabel *lblTeamName
		//methods
		//Specify an action
 		- (IBAction) calculatePointsButtonPressed: (id) sender;
	
	

What is a delegate?

An delegate object is an object that acts on behalf of another object. (It may also act in coordination with another object.)

E.g. the UIApplication class implements behaviour for an application. It may require updates about the applications current state. To receive these notifications, we could create a subclass of UIApplication that implemented methods to deal with simple notifications.

However, a simpler more scalable approach is to create a protocol that defines these methods and then create a delegate object (class UIApplicationDelegate) that conforms to that protocol and implements its methods. It does so on behalf of the UIApplication object.

What is a Protocol?

An protocol declares methods that can be implemented by any class. If a class conforms to a protocol then it must implement the methods specified in that protocol.

The declaration of a protocol is similar to a class declaration, but obviously a protocol does not define any instance variables