Learn Objective-C from http://cocoadevcentral.com
Calling Methods
You can call a methon on object or class
[object method];
[object methodWithInput:input];
id myObject = [NSString string];
id type can refer to any kind of object.
a method name can splite to several segments, not named arguments.
-(BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
The method name is actually writeToFile:atomically: in the runtime system.
Accessors
All instance variables are private by default.
Traditional 1.x syntax:
[photo setCaption:@"Day at the Beach"];
output = [photo caption];
Objective-C 2.0 dot syntax:
photo.caption = @"Day at the Beach";
output = photo.caption;
Choose only one style for each project.
The dot syntax should only be used setters and getters, not for general purpose methods.
Creating Objects
Create a autoreleased object:
NSString* myString = [NSString string];
The nested method call way:
NSString* myString = [[NSString alloc] init];
NSNumber* value = [[NSNumber alloc] initWithFloat:1.0];
Basic Memory Management
If you create an object using the manual alloc style, you need to release the object later. You should not manually release an autoreleased object because your application will crash if you do.
Designing a Class Interface
#import directive automatically guards against including a single file multiple times.
The @interface says that this is a declaration of the class Photo.
Objective-C methods typically leave out the "get" prefix.
The compiler assumes a method returns an id object, and that all input values are id.
Class Implementation
There are actually two ways to free a reference to an object: release and autorelease. The standard release will remove the reference immediately. The autorelease method will release it sometime soon
We can create an init method to set inital values for our instance variables:
- (id) init
{
if ( self = [super init] )
{
[self setCaption:@"Default Caption"];
[self setPhotographer:@"Default Photographer"];
}
return self;
}
The dealloc method is called on an object when it is being removed from memory.
- (void) dealloc
{
[caption release];
[photographer release];
[super dealloc];
}
[super dealloc] to ask the superclass to do its cleanup. If we don't do this, the object will not be removed, which is a memory leak.
The dealloc method is not called on objects if garbage collection is enabled. Instead, you implement the finalize method.
More on Memory Management
By ARC, you alloc an object, maybe retain it at some point, then send one release for each alloc/retain you sent.
if you create an object with alloc or copy, send it a release or autorelease message at the end of the function. If you create an object any other way, do nothing.
Logging
NSLog ( @"The current date and time is: %@", [NSDate date] );
Properties
Properties are a feature in Objective-C that allow us to automatically generate accessors
In the .h file:
@property (retain) NSString* caption;
@property (retain) NSString* photographer;
In the .m file:
@synthesize caption;
@synthesize photographer;
Accessors will only be generated if the don't already exist.
Calling Methods on Nil
In Objective-C, the nil object is the functional equivalent to the NULL pointer,The difference is that you can call methods on nil without crashing or throwing an exception.
Categories
A category allows you to add methods to an existing class without subclassing it or needing to know any of the details of how it's implemented.
//.h
#import <Cocoa/Cocoa.h>
@interface NSString (Utilities)
- (BOOL) isURL;
@end
//.m
#import "NSString-Utilities.h"
@implementation NSString (Utilities)
- (BOOL) isURL
{
if ( [self hasPrefix:@"http://"] )
return YES;
else
return NO;
}
@end
//using
NSString* string1 = @"http://pixar.com/";
NSString* string2 = @"Pixar";
if ( [string1 isURL] )
NSLog (@"string1 is a URL");
if ( [string2 isURL] )
NSLog (@"string2 is a URL");