Objective-C Notes

Objective-C notes

Apple coding guidelines

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html

Tips

String Formatting

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

Message expression [receiver method:arguement]
Message [receiver method:arguement]
Selector [receiver method: arguement]
Method The code selected by a message

dealloc

dealloc order

- (void)dealloc
{
  ...
  [titleLabel dealloc];
  [urlLabel dealloc];
  ...
  [super dealloc];
}

Set properties to nil in dealloc

- (void)dealloc
{
  ...
  self.rootViewController = nil;
  self.window = nil;
  self.someVariable = nil;
  ...
}

Use autorelease

// Replace:
NSString *text = [ NSString stringWithFormat:@"%0.2f", floatValue]

// With:
NSString *text = [ [ [ NSString alloc] initWithFormat:@"%0.2f", floatValue ] autorelease ];
NSNumber n1 = [[NSNumber alloc] initWithFloat:1.5f] autorelease];
NSNumber n2 = [NSNumber numberWithFloat:1.5f];

arrayWithObjects: vs arrayWithObject:

// arrayWithObjects
NSArray *n1 = [NSArray arrayWithObjects: blah, blah2, nil];

// arrayWithObject
NSArray *n2 = [NSArray arrayWithObject: blah];

Using pragma mark

#pragma mark
#pragma mark UIPickerViewDelegate

Controller Notes

//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
//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

//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
//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

Browse Space

- Pages
- Blog
- Labels
- Attachments
- Bookmarks
- Mail
- Advanced

Explore Confluence

- Popular Labels
- Notation Guide

Your Account

Log In

 

Other Features

Add Content