Learn Objective C http://cocoadevcentral.com/d/learn_objectivec/ Objective C Objective C Mac C Objective CC C Scott Stevenson [object method]; [object methodwithinput:input]; output = [object methodwithoutput]; output = [object methodwithinputandoutput:input]; NSSting string NSString id myobject = [NSString string]; id myobject NSString
NSString* mystring = [NSString string]; NSString NSString * Objective-C id function1(function2()); function2 function1 Objective-C [NSString stringwithformat:[prefs format]]; Objective-C : -(BOOL)writeToFile:(NSString *)path atomically:(bool)useauxiliaryfile; BOOL result = [mydata writetofile:@ /tmp/log.txt atomically:no]; writetofile:atomically: Objective C 1.x [photo setcation:@ Day at the Beach ]; output = [photo caption]; 2caption Objective-C getter get
Mac OS X 10.5 Objective-C 2.0setter getter photo.caption = @ Day at the Beach ; output = photo.caption; setter getter NSString* mystring = [NSString string]; (autoreleased) NSString* mystring = [[NSString alloc] init]; NSStringalloc init init init NSNumber* value = [[NSNumber alloc] initwithfloat:1.0];
Mac OS X alloc release (release) (autoreleased) //string1 NSString* string1 = [NSString string]; // NSString* string2 = [[NSString alloc] init]; [string2 release]; Objective-C interfaceclassname.h public ClassName.m private Photo Photo.h:
@interface Photo : NSObject NSString* caption; NSString* photographer; Cocoa.h Cocoa #import @interface Photo(superclass) NSObject caption photographer NSString id getter @interface Photo : NSObject NSString* caption; NSString* photographer; - caption; - photographer; Objective-C get (-) (+) static id id @interface Photo : NSObject NSString* caption; NSString* photographer; - (NSString*)caption;
- (NSString*)photographer; (setter) @interface Photo : NSObject NSString* caption; NSString* photographer; - (NSString*)caption; - (NSString*)photographer; - (void) setcaption: (NSString*)input; - (void) setphotographer: (NSString*)input; void, (getter) #import "Photo.h" @implementation Photo - (NSString*) caption return caption; - (NSString*) photographer return photographer; @implementation
- (void) setcaption: (NSString*)input [caption autorelease]; caption = [input retain]; - (void) setphotographer: (NSString*)input [photographer autorelease]; photographer = [input retain]; - (void) setcaption: (NSString*)input caption = input; release retain release autorelease release autorelease autorelease Init init - (id) init if ( self = [super init] ) [self setcaption:@"default Caption"]; [self setphotographer:@"default Photographer"]; return self; = [super init] self
if Dealloc dealloc (void) dealloc [caption release]; [photographer release]; [super dealloc]; release autorelease release [super dealloc] dealloc finalize Objective-C alloc retain release alloc retain release 1 2 setter autorelease retain dealloc release alloc copy
release autorelease - (void) settotalamount: (NSNumber*)input [totalamount autorelease]; totalamount = [input retain]; - (void) dealloc [totalamount release]; [super dealloc]; alloc NSNumber* value1 = [[NSNumber alloc] initwithfloat:8.75]; NSNumber* value2 = [NSNumber numberwithfloat:14.78]; // only release value1, not value2 [value1 release]; NSNumber* value1 = [[NSNumber alloc] initwithfloat:8.75]; [self settotal:value1]; NSNumber* value2 = [NSNumber numberwithfloat:14.78]; [self settotal:value2]; [value1 release]; setter Objective-C 90% Objective-C NSLog() C printf() %@ NSLog ( @"The current date and time is: %@", [NSDate date] );
NSLog description NSString Properties caption author Objective-C Photo @interface Photo : NSObject NSString* caption; NSString* photographer; - (NSString*) caption; - (NSString*) photographer; - (void) setcaption: (NSString*)input; - (void) setphotographer: (NSString*)input; @interface Photo : NSObject NSString* caption; NSString* photographer; @property (retain) NSString* caption; @property (retain) NSString* photographer; @property Objective-C retain setter #import "Photo.h"
@implementation Photo @synthesize caption; @synthesize photographer; - (void) dealloc [caption release]; [photographer release]; [super dealloc]; @synthesize setter getter dealloc @synthesize @synthesize getter setter Nil Objective-C nil NULL nil nil nil dealloc - (void) dealloc self.caption = nil; self.photographer = nil; [super dealloc]; nil (setter) retain nil dealloc self.<var>setter // incorrect. causes a memory leak. // use self.caption to go through setter caption = nil;
Category Objective-C NSString NSString URL @interface NSString (Utilities) - (BOOL) isurl; URL #import "NSString-Utilities.h" @implementation NSString (Utilities) - (BOOL) isurl if ( [self hasprefix:@"http://"] ) return YES; else return NO;
LearnObjectiveC Xcode 3.0 Project (56k) http://cocoadevcentral.com/downloads/learnobjectivec-20080414a. zip