Objective-C Notes
Objective-C notes
Apple coding guidelines
Tips
- Control "click" on File's Owner to see available "Outlets" and "Received Actions".
- Control "." when typing will cycle through code completion suggestions.
String Formatting
- Objective-C uses the C printf style syntax:
NSLog(@"string: %@", @"ABCD"); // or %s NSLog(@"number1: %d", 123456); NSLog(@"number2: %05d", 89); NSLog(@"hex: %x", 255); NSLog(@"float: %5.2f", 3.14159); NSLog(@"unsigned: %u", 250); NSLog(@"boolean: %@", boolValue ? @"YES" : @"NO"); NSLog(@"NSNumber: %@", someNSNumberValue);
Terminology
- From Stanford lecture slides
| Message expression | [receiver method:arguement] |
| Message | [receiver method:arguement] |
| Selector | [receiver method: arguement] |
| Method | The code selected by a message |
dealloc
- If you alloc some object, it is your responsibility to dealloc it.
dealloc order
- Put local properties first, before any super calls.
- (void)dealloc
{
...
[titleLabel dealloc];
[urlLabel dealloc];
...
[super dealloc];
}
Set properties to nil in dealloc
- In the dealloc class methods set all properties to nil.
- This is important if other classes are accessing the properties, makes sure things are reset.
- (void)dealloc
{
...
self.rootViewController = nil;
self.window = nil;
self.someVariable = nil;
...
}
Use autorelease
- Action: Use alloc - init - autorelease when creating NSStrings, NSNumbers etc.
- Reason: System manages releasing the objects for you.
// Replace: NSString *text = [ NSString stringWithFormat:@"%0.2f", floatValue] // With: NSString *text = [ [ [ NSString alloc] initWithFormat:@"%0.2f", floatValue ] autorelease ];
- What is there a difference between these calls?
- Is the numberWithFloat autoreleased too?
NSNumber n1 = [[NSNumber alloc] initWithFloat:1.5f] autorelease]; NSNumber n2 = [NSNumber numberWithFloat:1.5f];
arrayWithObjects: vs arrayWithObject:
- arrayWithObjects: requires a nil to end the set.
// arrayWithObjects NSArray *n1 = [NSArray arrayWithObjects: blah, blah2, nil]; // arrayWithObject NSArray *n2 = [NSArray arrayWithObject: blah];
Using pragma mark
- Group delegate methods together
- Group methods overridden from each parent class together
- Group new methods by functionality
#pragma mark #pragma mark UIPickerViewDelegate
Controller Notes
- header should look like:
//MyViewController.h #import <UIKit/UIKit.h> @interface MyViewController : UIViewController <UITextFieldDelegate> { IBOutlet UISwitch *switchTest; IBOutlet UILabel *switchLabel; } @property (nonatomic, retain) UISwitch *switchTest; @property (nonatomic, retain) UILabel *switchLabel; - (IBAction)changeSwitchLabel:(id)sender; @end
- class should look like:
//MyViewController.m #import "MyViewController.h" @implementation MyViewController @synthesize switchTest; @synthesize switchLabel; - (IBAction)changeSwitchLabel:(id)sender{ // Do something. } - (void)viewDidLoad { // Do something on start. } - (void)dealloc { [switchTest release]; [switchLabel release]; [super dealloc]; } @end
Delegate Notes
- header should look like:
//HelloWorldAppDelegate.h #import <UIKit/UIKit.h> @class HelloWorldViewController; @class MyViewController; @interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate> { IBOutlet UIWindow *window; MyViewController *myViewController; } @property (nonatomic, retain) UIWindow *window; @property (nonatomic, retain) MyViewController *myViewController; @end
- class should look like:
//MyViewController.m #import "MyViewController.h" #import "HelloWorldAppDelegate.h" @implementation HelloWorldAppDelegate @synthesize window; @synthesize myViewController; - (void)applicationDidFinishLaunching:(UIApplication *)application { MyViewController *aViewController = [[MyViewController alloc] initWithNibName:@"ControllerView" bundle:[NSBundle mainBundle]]; self.myViewController = aViewController; [aViewController release]; UIView *controllersView = [myViewController view]; [window addSubview:controllersView]; // Override point for customization after app launch [window makeKeyAndVisible]; } - (void)dealloc { [myViewController release]; [window release]; [super dealloc]; } @end