Objective-C Language Tutor : Michael
Hello Obj-C #import <Foundation/Foundation.h> int main(int argc, const char * argv []){ NSAutoreleasePool* pool = [[NSAutoreleasePool alloc ] init]; NSLog(@"hello obj-c"); } [pool drain]; return 0;
Terminal 1. gcc -framework Foundation xxx.m -o prog 2../prog
First two things NSLog() is like printf() in C Use obj-c type @ string instead of c type string
Start from XCode
Foundation
Create Project
Select Main file
Run
Function call #import <Foundation/Foundation.h> BOOL result(int value1, int value2){ if(value1 == value2) return YES; else return NO; } NSString *string (BOOL boolresult){ if(boolresult == YES) return (@"YES"); else return (@"NO"); } int main (int argc, const char * argv[]) { int a = 5; int b = 5; NSLog(@"Is %d equal to %d? %@", a, b, string(result(a,b))); b = 7; NSLog(@"Is %d equal to %d? %@", a, b, string(result(a,b))); return 0; }
Result
Recap #import <Foundation/Foundation.h> void modifiedpointer(int * a){ int * b; b =a ; *b = 3; } int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; } int * a; int b = 9 ; a = &b; modifiedpointer(a); NSLog(@"Result is %d", *a); [pool drain]; return 0; Result is?
(Object)
30
Class( )
!!
Class Object
Class object object class
Object? Class
Object Car
Behavior Car race behavior
State Car engine wheel race state behavior
Message Car doraceing race
Other object as state Engine Honda Car engine wheel race
Object in code Application class class class object instance instance instance
Class - Object - instance,, Name - State - attribute, member,,, Behavior - method, action,,
Class & Instance in C class - instance - struct BookInfo { int rating; char title[titlelength]; char author[authorlength]; }; int main (int argc, const char * argv[]) { struct BookInfo mybook; }
int a ; // a struct BookInfo b ; // b (int a;) function c struct function
Class in Objective-C - interface - implementation Class Object
Class - @interface Shape : NSObject { int color; } - (void) setcolor: (int) Color; - (void) draw; @end - (void) setcolor: (ShapeColor) Color withnum:(int) anum;
Class - @implementation Shape - (void) setcolor: (int) mycolor { color = mycolor; } @end
Function - void myplus(int a, int b); int main (int argc, const char * argv[]) { myplus(4, 5); } void myplus(int a, int b){ printf("a + b is %d\n", a+b); }
Method - @interface Shape : NSObject { ShapeColor color; } - (void) setcolor: (ShapeColor) Color; - (void) draw; @end int main (int argc, const char * argv[]) { Shape * s = [Shape new]; [s setcolor:blue]; [s draw]; } @implementation Shape - (void) setcolor: (ShapeColor) mycolor { color = mycolor; } @end
Class Diagram Shape color setcolor: draw
Recap - My Class typedef enum { red, green, blue, }ShapeColor; @interface Shape : NSObject { ShapeColor color; } - (void) setcolor: (ShapeColor) Color; - (void) draw; @end @implementation Shape - (void) setcolor: (ShapeColor) mycolor { color = mycolor; } - (void) draw { NSLog(@ draw some shape %d, color); } @end
Recap - Usage #import <Foundation/Foundation.h> // int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; Shape * s = [Shape new]; [s setcolor:blue]; [s draw]; } [pool drain]; return 0; Result is?
Objective-C method & C function Obj-C style : infix notation [s setcolor: yellow ] [someobj settitle: @ sometitle setkey:556677 ] Method name - (settitle:setkey:) C style settitleandkey(someobj, @ sometitle, 556677); Function name - settitleandkey
Variable - NSStirng * name = [[NSString alloc ] init]; [[NSString alloc ]init] [NSString new] [aobj something] something aobj
Shape int color Shape color method Shape color 5ff344
App Press Label button label pressed: Controller
Target / Action Press target action pressed: Label button label pressed: Controller
Demo HelloXcode
Basic Terms Class : Object Instance : Class, object Method : Object Instance Variable (ivar): Object
OO Terms Encapsulation Polymorphism object Inheritance hierarchical organization, share code, customize or extend behaviors
Inheritance Cat - Subclass inherits states and behaviors from superclass Superclass leg run: - Subclass may augment, replace the superclass methods Subclass Leopard leg roar: run:
Class Diagram Shape color setcolor: draw Circle draw
Recap - Inheritance typedef enum { red, green, blue, }ShapeColor; @interface Shape : NSObject { ShapeColor color; } - (void) setcolor: (ShapeColor) Color; - (void) draw; @end @interface Circle : Shape @implementation Shape - (void) setcolor: (ShapeColor) mycolor { color = mycolor; } @end @implementation Circle - (void) draw { - (void) draw { NSLog(@ draw some shape %d, color); } @end NSLog (@ Drawing circle... color is %d, color); } @end
Recap - Inheritance // int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; Circle * c = [Circle new]; [c setcolor:green]; [c draw]; } [pool drain]; return 0; Result?
Message Dispatching Shape color 3. dispatch superclass setcolor: draw 1. message Circle c; [c setcolor:yellow]; Circle draw 2. class setcolor:
Inherited Instance Shape color setcolor: draw Memory Layout of Circle object isa... color radius Circle NSObject Shape Circle Circle radius draw
Class & Instance Blueprint White house
Declare & Implement Hello.h Hello.m.h.m
Hello.h @interface Hello : NSObject { int age; } -(void) greeting:(nsstring *) words; @end
Hello.m #import Hello.h @implementation Hello -(void) greeting:(nsstring *) words{ NSLog(words); } @end
Message Syntax
Message Syntax [object signal] [object signal:argument] [object signal:arg1 withother:arg2]
Message Syntax - More [object signal] // object signal [object signal:argument] // object signal argument object [object signal:arg1 withother:arg2] // arg1 arg2 object // signal:withother: object object
Terminology Message Expression [object signal:argument] Message [object signal:argument] Selector Receiver [object signal:argument] Method message codes
Examples of Message Consumer *customer; // [customer buyonline]; int card = [customer creditcard]; if([customer iscardvalid]){ // } [customer buyitem:@ Macbook quantity:1]; NSString *address = [[customer bill] address];
Class & Instance Method instance method - (id) init; // e.g. [[Hello alloc ] init ] - (void) greeting:(nsstring *) word; class method + (id) alloc; // e.g. [Hello alloc] + imagenamed:(uiimage) image; // [UIImage imagenamed:@ hello.png ]
Class & Instance Example [NSString string]; // string is +/-? [@ 500 intvalue ]; // intvalue is +/-? [[NSArray alloc ] initwithobject:@ fine ]; // initwithobject: is +/-? [NSArray arraywithobject:@ ok ]; // arraywithobject: is +/-?
Convenient Way - Since Objective-C 2.0 - Dot Syntax NSString *name = person.name; // name = [person name] person.name = @ Michael ; // [person setname:@ Michael ] - Cascade person.bill.name = @ Andy ; // [[person bill] setname:@ Andy ] person.bill.name ; // [[person bill] name]
Demo ModifyClass Getter & Setter
Types
Dynamic & Static - Dynamic id someobject; // id * - usually use id only - Static Customer *person; - Objective-C checks type at compile-time, usually uses run-time binding
Null pointer to Object Check exsiting if(person!= nil){} if(!person){} Usage person = nil; // assignment [child setfather:nil]; // as argument
Boolean Type Declare Usage BOOL lock; lock = YES; // true lock = NO; // false if(lock!= YES) if(lock) if(!lock)
Selector - choose method by name - Type Define SEL sel; - Usage SEL sel = [object work]; SEL sel = @selector(work:); SEL sel = @selector(work:who:); [object setaction:@selector(work:)];
Object works with selector id obj; SEL action = @selector(work); - Check response if([obj responsetoselector:action]) - Perform Selector [obj performselector:action]; // [obj work] [obj performselector:action withobject:who];
Demo Selector performselector
Operation
With Class - Feature Class me = [obj class]; NSString *name = [obj classname]; - Relationship if([obj iskindofclass:[uiview class]]){} // if obj inherit UIView if([obj ismemberofclass:[nsstring class]]){} // if obj is the instance of NSString
With Object - Check identity if(obj1 == obj2){ // the same object instance } - Check equality if([obj1 isequal: obj2]){ } // the same content, define by user
Overwrite -description - (NSString *) description; - Usage NSLog([@ test stringbyappendingformat:@"%@, obj]); // call [obj description]; - User can overwrite description to get the specific information NSLog([obj description]);
Numeric Type
- 32-bit - 64-bit NSInteger (int or long) NSUInteger CGFloat (float or double)
Question?