Wednesday, February 25, 2009

Basic Objective-C object syntax

Some notes I have taken based on the short tutorial I found here.

Whenever you see code inside square brackets, you are sending a message to an object or a class.

To create a new object:

id myObject = [NSString string];


The id type means that the myObject variable can refer to any kind of object, so it won't be checked at compile time. However in this case we know the object type will be an NSString, so it could be written it like this:

NSString* myString = [NSString string];


myString is now an NSString variable, so the type will be checked by the compiler. This way of creating an object creates an autorelease object, which will automatically free the object's memory. To create an object using the manual style do it like this:


NSString* myString = [[NSString alloc] init];


If you create an object using the manual alloc style, you need to release the object later:

[myString release];


This (manual) way of object instantiation uses a nested method call (see below). The alloc method called on NSString reserves memory and instantiates an object. The call to init does basic setup, such as creating instance variables. In some cases, you may use a different version of init which takes input:

NSNumber* value = [[NSNumber alloc] initWithFloat:1.0];

Note the asterisk to the right of the object type. All Objective-C object variables are pointers types. The id type is predefined as a pointer type, so there's no need to add the asterisk.

To call a method on an object:

[object method];
or
[object methodWithInput:input];

To return a value:

output = [object methodWithOutput];
or
output = [object methodWithInputAndOutput:input];

To write a nested method, the format is:

[object methodWithInput:[object2 methodWithInput2]];

Monday, February 23, 2009

Some basic take-aways from Objective-C

A few key points I need to remember...

  • You usually want to look at your application delegate’s applicationDidFinishLaunching:initWithCoder: and viewDidLoad as places to add custom start-up behavior.

  • Obj-C method declarations go in the header file, outside the @interface block, in the form:

    - (returntype) methodName: (parameter1type) parameter1 parameter2Name: (parameter2type) parameter2... ;

  • Declare properties with a @property declaration in the header file, then use @synthesize in the implementation to have the compiler create the getters and setters for you.

  • Your code should never call dealloc directly; it will be called automatically when an object is being freed by the Objective-C runtime.

  • You own any object you obtain by way of a method with alloc, new, or copy in its name, and must eventually release (or autorelease) such objects. You do not own objects obtained through other means, and must not release them, unless you choose to become an owner by calling retain.

Sunday, February 15, 2009

Second App

I will never pick this up at this rate. I am on exercise TWO only, after three weeks. My life and work are not conducive to learning right now....

Success, in any case, on my second try. A few notes about what I have learned so far. In


NSString* helloMessage = [[NSString alloc] initWithFormat: @"Hello %@", userName ];

memory is allocated for the object with alloc, and then the contents are initialized. To create the string substitution, a format string is used in the form Hello %@. The leading @ allocates a static NSString. The %@ is a format specifier that allows me to insert a string representation of any Cocoa object, userName in this case.

Saturday, January 24, 2009

I got a Mac

I got a Mac Mini this week (thanks Anoop!). Now I can start learning in earnest.

Apple makes available a variety of resources for the developer just starting out on the iPhone platform. There are some useful introductory videos, documents and code samples in the iPhone DevCenter. I haven't had a lot of time to spend on learning this week since I have become busier with my regular job. However, I can recommend the videos.

I did download and install the iPhone SDK. There are a number of tools available in the SDK, such as the IDE -- called Xcode -- and the iPhone Simulator, which mimics the behavior of your apps on a real iPhone (or iPod Touch). Using these tools and following the tutorial from the iPhone SDK Development book I pretty quickly created my first app.

Tuesday, January 13, 2009

Rewind

I have been reading a bit about Objective-C. It turns out that it is a superset of the C programming language; that is, it extends C. So my first act is not to learn Objective-C, but to relearn C.

I haven't cracked my Kernighan and Ritchie in a decade, and I have to admit that I am not looking forward to it. One of the main reasons I jumped on the Java bandwagon was that I didn't have to deal with memory management. Ugh.

Sunday, January 11, 2009

How to start

I need a Mac. A Mac with an Intel CPU. The iPhone SDK requires it. It would be nice if Apple allowed Mac OS X to be virtualized, but then I guess fewer people would buy their sleek but crappy hardware.

Don't get me wrong: I am a huge fan of Mac OS X, which is Unix-based, and Apple's attention to design interfaces. It is their hardware I take issue with. Cheap, proprietary, expensive. Don't buy it without an extended warranty. You have been warned.

I don't need the latest-and-greatest. just the least-expensive hardware that will hold OS X on an Intel architecture. I am thinking about buying a new $600 Mac Mini -- with the extended warranty, of course. I won't need keyboard, mouse or monitor, as I can simply remote in via VNC.

In the beginning...

In the beginning there was nothingness. No, really. I don't know how to do this at all. I can code in Java, JavaScript, SQL, HTML and some VBScript. Coding for the iPhone will require new skills.

There are two ways to create software applications for the iPhone platform: native applications written in Objective-C, and web applications that can be accessed via browser (especially interesting is the iUI library). My plan is to learn both, and to document my progress herein.