RSS

iPhone Development: Objective-C Primer

March 11th, 2008 Posted in Software Development, iPhone

This article is a summary of what I learned from reading Learning Objective-C: A Primer on the Apple iPhone developer web site. You’ll probably need to be registered and logged in to the Apple developer web site to access this link.

This is my first exposure to Objective-C and development on an Apple platform. I’ve been developing software for nearly 20 years and have worked in various languages include BASIC, Pascal, Modula-2, C, C++, Assembly, FORTRAN, PHP, C#, Visual Basic, ADA, List and various scripting languages.

I have to say my first thought on this language is that it is kind of archaic compared to modern languages like C# and Java. C++ is basically C with object oriented functionality added to it. Objective-C is the same thing really but done differently. I think C++ does a lot better job with the syntax. Objective-C looks like a hack to me. I haven’t actually used it yet so I may find that I like it better than C++.

Just like C and C++ there are .h header files. Instead of .c source files there are .m source files. The document also mentions .mm source files that you can use for C++. So does that mean you can use C++ instead of Objective-C?

You can use #include to include header files or you can use #import which is the same as include but will only include the header once. That’s nice so you don’t need a #define or a #pragma once.

NSString is a string data type and you can assign literals to it with @”literal” syntax. It has built in memory management. Also object variables are always declared as pointers. No objects on the stack I guess.

Classes are declared like this:

@interface ClassName : BaseClass
{
    Member variables declarations
}
Method delcarations
@end

@implementation ClassName
    Method implementations
@end

You can reference objects both as strongly typed objects and weakly typed objects.

    ClassName * obj; // strong type
    id obj; // weak type

Methods can be instance methods or class methods. Class methods are similar to static methods in C++. Static methods can be called without having an instance of the class. They are generally used for object factories.

Here is how to implement a insertObject:atIndex instance method.

-(void)insertObject:(id)anObject atIndex:(NSUInteger)index

A - at the start declares the method as a instance method and a + would make it a class method. The first set of parenthesis contains the return type. The return type is followed by the method name which is followed by the first parameter. Next comes a list of named parameters. It looks like the name of the first parameter is the method name. The insertObject and atIndex name form a method signature. So I guess you could have methods with the same parameter signature that are different. You can’t do this with C++.

Calling methods is strange. When you call a method it is called “messaging” the object. It happens dynamically at run-time and if any sub-classes override a base class method they will be called first. The overriding method has the option to call the base class method. This is very C++ virtual function like except it appears that all methods are virtual in Objective-C.

Here is a method call:

[myArray insertObject:anObj atIndex:0]

The call is enclosed with square brackets. The object to call is on the left and is followed by the named parameters and values. You can also nest method calls. For example:

[[myAppObject getArray] insertObject:[myAppObject getObjectToInsert] atIndex:0]

In Objective-C the keyword for NULL is nil;

You can create properties. This is a quick way to create member variable accessors. This will generate getters and setters for member variables.

@property type name
@property BOOL flag
@property (copy) NSString* obj; //copy on access
@property (readonly) NSString* obj; // read only getter

You can declare them as copy on access or read only. There may be more access modifiers but this document didn’t mention them.

You can use standard dot (.) notation to access properties or do it like a method call.

obj.flag = YES;
or
[obj setFlag:YES];

Protocols are the equivalent of .NET or Java interfaces. They define a set of methods but have no implementation, base class or member variables.

@protocol MyProtocol
-(void)method;
@end

Delegates implement protocols and are like callback classes you can use to receive “messages” from classes.

This document was a good introduction to the differences between Objective-C and C++. The syntax is foreign to me but the functionality is very similar. I saw no mention of a public/private/protected type access modifiers or multiple inheritance.

[del.icio.us] [Digg] [Reddit] [Technorati]

RSS feed | Trackback URI

Comments »

No comments yet.

Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.