Thursday, July 9, 2009

Objective-C Tutorial at CocoaDevCentral

I just finished reading the nice and brief article on learning Objective-C over at http://www.cocoadevcentral.com/d/learn_objectivec/.

Check it out!


http://www.cocoadevcentral.com/d/learn_objectivec/

Believe in your 'self' but be careful when assigning.

self in Objective-C is analogous to this in C++ and Java.

name = @"Jed"; // accesses the instance variable directly
self.name = @"Jed"; // access the setter method of the instance variable.

// again, from stanford course
@implementation Person
- (void)setAge:(int)newAge {
[self setAge:newAge]; // Infinite loop!
}
@end

Objective-C policies for properties:

From the stanford course:

Read-only vs. read-write:
@property int age; // read-write
@property (readonly) BOOL isHappy; // read-only

Memory management policies for object properties (not primitives)
@property (assign) NSString *name; // pointer assignment
@property (retain) NSString *name; // retain called
@property (copy) NSString *name; // copy called

setters/getters vs. properties

In Objective-C it is common to provide accessor (getter) and mutator (setter) methods like in the following:

@interface RunningShoe : NSObject {
// instance variables
NSString* shoeName;
}

// method declarations
-(NSString *) shoeName;
-(void) setShoeName:(NSString*) _someShoeName;

@end

and the implementation:

@implementation RunningShoe

- (void)setShoeName:(NSString *)value {
if (value != shoeName) {
[value release];
name = [value copy];
}
}

@end


This is kind of wordy after a while. Well, there is a simpler alternative called properties:


@interface RunningShoe : NSObject {
NSString* shoeName;
}

// define properties
@property (copy) shoeName;

@end

now the implementation:

@implementation RunningShoe

@synthesize shoeName;
@end

One of the cool things about properties is that they are overidable. You may be happy with the standard getter/setter functionality...but you may want to do something out of the ordinary. Perhaps you want to capitalize a username property before actually assigning it to an instance variable. By overriding the setUsername method you can do this (even though it is a @synthesize property).

Cocoa with Love

Begun reading a Matt Galagher's page called Cocoa with Love.

Good-day mate!

Objective-C 'description' like Java toString...

If you have done any programming in Java you may be aware of the method that will return a String representation of an object:

String toString() {
return "some string";
}

In Objective-C there is a similar class method on NSObject called description.

From the documentation description: "returns a string that describes the contents of the receiver."


WARNING: just because NSObject defines description does not mean the runtime will automatically recognize your override of the method UNLESS you explicitly declare it in your header (.h) file.

The header (.h) file
@interface MyObj : NSObject {
// instance variables
}
-(NSString *) description;

@end

Now the implementation (.m) file:
@implementation MyObj {
// instance variables
}
-(NSString *) description {
return @"My special override of description";
}

@end


Now you can do something like the following:

...
MyObj mo = [[MyObj alloc] init];

NSLog( @"Your object is: %@", mo );
...

Output:

Your object is: My special override of description


You say Either I say I-ther, You say AppKit I say UIKit

UIKit is the User Interface framework on the iPhone.

If you were creating a regular MAC-OSX you would use the AppKit.