You are on page 1of 28

Lp trnh ng dng trn iOS

Objective-C

Ni dung
Gii thiu v Objective-C Quy tc t tn lp, tn phng thc, tn bin Class trong Objective-C Phm vi truy xut cc bin th hin Property trong Objective-C Phng thc trong Objective-C Category trong Objective-C Protocol trong Objective-C K tha X l ngoi l Mt s cu trc iu khin trong Objective-C Qun l b nh Selector trong Objective-C

Gii thiu v Objective-C


Ngn ng lp trnh hng i tng ANSI C + Smalltalk = Objective-C Objective-C l ngn ng lp trnh chnh c Apple chn vit cc ng dng cho h iu hnh Mac OS, iOS --> D dng nm bt c ngn ng Objective-C nu c kinh nghim v C

Quy tc t tn
Class Method Variable
- (void) findMe;

Bt u bng k t hoa

@interface MyClass : NSObject { } @end

Bt u bng k t thng
NSString *stringURL;

Ging method
MyClass.h @interface MyClass : NSObject{ NSString *stringURL; } - (void) findMe; @end

Class trong Objective-C


@interface: Khai bo class @implementation: nh ngha class @end: Kt thc khai bo class hoc kt thc nh ngha class
// .h @interface MyClass : NSObject { // } @end // .m @implementation MyClass // @end

Phm vi truy xut cc bin


@private: Gii hn phm vi trong lp m bin th hin c khai bo @protected (default): Gii hn phm vi trong lp v lp con k tha m bin th hin c khai bo @public: Khng gii hn phm vi truy xut

Phm vi truy xut cc bin (tt)


V d:

Property trong Objective-C


Property cho php nh ngha cc b truy xut (setter/getter) --> Thun li cho vic truy xut n cc bin th hin nh ngha trong file .h
@property (<attributes>) type propertyName;

Thc thi trong file .m


@synthesize propertyName;

Property trong Objective-C (tt)


Cc thuc tnh (attributes)
readwrite (default): c v ghi (get/set) read-only: ch c th c (get) assign (default): Thng dng vi cc kiu v hng nh NSInteger, CGFloat, CGrect, retain: Thng c s dng cho i tng copy: To mt bn sao ca i tng s c s dng cho i tng ch nh atomic (default): Thc hin ng b ho nonatomic: Ngc vi atomic

Property trong Objective-C (tt)


Cc thuc tnh (attributes)
- atomic
// @property (retain) UITextField *username; // Generates roughly - (UITextField *) username { UITextField *retval = nil; @synchronized(self) { retval = [[username retain] autorelease]; } return retval; } - (void) setUsername : (UITextField *) _username { @synchronized(self) { [_username retain]; [username release]; username = _username; } }

Property trong Objective-C (tt)


Cc thuc tnh (attributes)
- nonatomic
// @property (retain, nonatomic) UITextField *username; // Generates roughly - (UITextField *) username { return username; } - (void) setUsername : (UITextField *) _username { [_username retain]; [username release]; username = _username; }

Phng thc trong Objective-C


Class method
K hiu du + Truy xut thng qua tn lp Khng th truy cp vo cc bin th hin (Instance variables)

Instance method
K hiu du Truy xut thng qua i tng

Phng thc trong Objective-C (tt)


Khai bo phng thc:
-/+ (return_type) method; -/+ (return_type) methodPara1 : (type) para1; -/+ (return_type) methodPara1 : (type) para1 andPara2: (type) para2;

Gi phng thc:

// Class method MyClass *myClass = [MyClass method]; // Instance method [object method]; [object methodPara1:pa1]; [object methodPara1:pa1 andPara2:pa2];

Category trong Objective-C


Khi mun thm mt s phng thc vo mt lp c sn
M rng lp bng cch vit li m ngun M rng lp khng cn vit li m ngun --> Category
MyClass.h @interface MyClass : NSObject{ } - (void) showMe; @end MyClass.m @implementation MyClass - (void) showMe { // show me } @end

Category trong Objective-C (tt)


Thm mt phng thc newMethod cho lp MyClass thng qua Category
MyClass+MyCategory.h @interface MyClass (MyCategory) - (void) newMethod; @end MyClass+MyCategory.m @implementation MyClass (MyCategory) - (void) newMethod { // to do new method } @end Ch : - Tn ca Category l duy nht (Khng c trng) - Trong Category khng cho php thm cc bin th hin

Procol trong Objective-C


Protocol l g ? V d:

@protocol MyProtocol // required (default) - (void) requiredMethod; @optional - (void) anOptionalMethod; - (void) anotherOptionalMethod; @required - (void) anotherRequiredMethod; @end

Person

K tha
Tng t nh cc ngn ng khc Teacher Trong Objective-C, root class ca tt c cc class l NSObject
Person.h @interface Person : NSObject { NSString *name; NSString *birthday; } // @end Teacher.h @interface Teacher : Person{ float fSalary; } // @end

X l ngoi l

Mt s cu trc iu khin
If For Switch While

Qun l b nh
Cc nguyn tc qun l b nh Vn khi khi to i tng Release Retain Dealloc Tham chiu yu

Qun l b nh
Cc nguyn tc qun l b nh
Nguyn tc cn bn
Khi bn nm quyn s hu mt i tng, khi to i tng bng cc phng thc m trong tn bt u vi vi alloc hoc new hoc copy (v d, alloc, newObject hoc mutableCopy) hoc gi mt thng ip retain, bn phi c trch nhim gii phng quyn s hu i tng bng cch s dng release hoc autorelease. Bt k khi no bn nhn c mt i tng (khng phi t mnh khi to), bn khng c release n. Khi bn cn lu tr mt i tng c nhn nh mt property trong mt bin th hin, bn phi retain hoc copy n. (iu ny khng ng cho cho tham kho yu, nhng y l in hnh him). Mt i tng c nhn thng m bo vn c hiu lc trong phng thc m n c nhn (ngoi tr trong cc ng a lung v vi trng hp Distributes Objects). Phng thc c th tr v an ton i tng m n c triu gi. S dng retain trong vic kt hp vi release hoc autorelease khi cn thit bo v mt i tng khi hiu lc ca cc thng ip khng hp l bn ngoi. autorelease c ngha l gi mt thng ip release sau

Nguyn tc khc

Qun l b nh
Vn khi khi to i tng
MyClass *myClass = [MyClass alloc]; [myClass init]; if(myClass) { // }

???

Qun l b nh
Vn khi khi to i tng (tt)
MyClass *myClass = [[MyClass alloc] init]; if(myClass) { // }

OK
Ch : - Lun lun tr v nil trong phng thc init nu c li xy ra - Phi kt hp 02 li gi phng thc alloc v init

Qun l b nh
Release
Ch c release khi khi to i tng mt cch th cng alloc KHNG release th cng mt i tng autorelease --> CRASH
// Must release string1 when done NSString *string1 = [[NSString alloc] init]; // [string1 release];

Autorelease

release

// string2 will be released automatically NSString *string2 = [NSString string];

Qun l b nh
Retain
Mi i tng c mt b m c s dng kim sot tt c cc tham chiu bi i tng hoc n c Phng thc alloc, new, copy v retain u lm tng b m ln 1 Phng thc release gim b m i 1 Khi b m c gi tr bng 0 --> Phng thc dealloc ca i tng s c gi xc nh gi tr ca b m: [object retainCount]

Qun l b nh
Dealloc
c gi khi i tng ang c remove khi b nh Nu mt lp c cc bin th hin (instance variable) l cc i tng th trong phng thc dealloc ca lp phi thc hin giii phng cc bin th hin ny
- (void) dealloc { [childVar1 release]; [childVar2 release]; // [super dealloc]; }

Qun l b nh
Tham chiu yu
To tham chiu n i tng m khng cn tr i tng t gii phng chnh n --> Thit lp tham chiu yu n i tng

Selector trong Objective-C


Selector trong Objective-C c 02 ngha:
Ch n tn ca mt phng thc khi n c s dng trong m ngun mt thng ip gi n mt i tng Ch n mt nh danh duy nht m thay th cho mt tn khi m ngun c bin dch