You are on page 1of 57

Lp trnh ng dng trn iOS

Phn 1: Gii thiu Objective-C

KHIEMBT@FPT.COM.VN

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 v cc bin th hin. Property trong Objective-C. Phng thc trong Objective-C.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Ni dung (tt)
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.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Gii thiu v Objective-C


Ngn ng lp trnh hng i tng. ANSI C + SmallTalk-80 = Objective-C. Objective-C l ngn ng lp trnh chnh c Apple chn vit cc ng dng trn h iu hnh Mac OS, iOS. => Lp trnh vin d dng nm bt ngn ng Objective-C nu c kin thc v C.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Quy tc t tn
Class - Bt u bng k t hoa. Method - Bt u bng k t thng.
@interface MyClass : NSObject //do some thing @end -(void)findMe;

Variable -Bt u bng k t thng.

NSString *stringURL;

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


LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Class trong Objective-C


@interface - Khai bo class. @implementation - nh ngha class.
//.h @interface MyClass : NSObject{ //do some thing } @end

@end - kt thc khai bo class


//.m @implementation MyClass //do some thing @end

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Phm vi truy xut cc bin


@private - Gii hn phm vi trong lp m bin th hin c khai bo. @protected - Gii hn phm vi trong lp v lp con k tha m bin th hin c khai bo. Nu khng khai bo phm vi truy xut bin th Objective-C t hiu l @protected.

@public - Khng gii hn phm vi truy xut.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Phm vi truy xut cc bin (tt)


V d:
@interface MyClass : NSObject { //private @private NSString *str1; //protected NSString *str2; @protected NSString *str3; //public @public NSString *str4; } @end
LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Property trong Objective-C


Property cho php nh ngha cc b truy xut (get/set) => Thun li cho vic truy xut n cc bin th hin. nh ngha trong file *.h @property(<attributes>) type propertyName; Ex: @property (nonatomic, assign) NSInterger tuSo; Th thi trong file *.m @synthesize propertyName; Ex: @synthesize tuSo;

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Property trong Objective-C (tt)


Truy cp <objectName>.<propertyName>; Ex: phanSoA.tuSo; phanSoA.tuSo = 2; Tng ng vi: [phanSoA tuSo]; [phanSoA tuSo : 2]; //get //set //get //set

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

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) khng cn gi li tham s truyn vo trc khi set, dng vi cc kiu v hng nh NSInterger, CGFloat, CGRect,
retain gi li tham s truyn vo trc khi set, thng c dng cho kiu d liu l cc lp i tng.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Property trong Objective-C (tt)


Cc thuc tnh (attributes) copy to ra mt bn sao ca tham s truyn vo trc khi set, thng dng khi l lp NSString.

atomic (default) thc hin ng b ho.


nonatomic ngc li vi atomic. strong (default) tham chiu mnh n tham s truyn vo. weak tham chiu yu n tham s truyn vo.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Phng thc trong Objective-C


Trong Objective-C c 2 loi phng thc: class method K hiu l du +

Truy xut thng qua tn lp.


Khng th truy cp vo cc bin th hin (instance variables). instance method K hiu l du Truy xut thng qua i tng.
LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Phng thc trong Objective-C (tt)


Khai bo phng thc.
Phng thc khng tham s
-/+(return_type) method;

Phng thc 1 tham s


-/+ (return_type) methodPara1 : (type) para1;

Phng thc nhiu tham s


-/+ (return_type) methodPara1 : (type) para1 andPara2: (type) para2;

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Phng thc trong Objective-C (tt)


Gi phng thc. Instance method [object method];

[object method Para1 : param1];


[object method Para1 : param1 andPara2 : param2]; Class method [class method]; [class method Para1 : param1];

[class method Para1 : param1 andPara2 : param2];


LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Category trong Objective-C


Objective-C cung cp tnh nng Category cho php m rng mt lp (vit thm method) m khng cn vo lp vit li code ang c sn. Ngay c khi khng c m ngun ca lp nhng vn hon ton c th thm phng thc cho lp qua thuc tnh ny. => Category gip gi m lnh sng sa, cho php m rng lp c mt cch linh hot v d bo tr trong nhng ln sau.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Category trong Objective-C (tt)


V d ta lp c bn l myClass c method info:
//myClass.h @interface myClass : NSObject{ NSString *name; } -(void)info; @end //myClass.m #import "myClass.h @implementation myClass -(void)info{ name = @"Bi Th Khim"; NSLog(@"Fullname: %@", name); } @end

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Category trong Objective-C (tt)


V by gi ta mun thm method workAt
//myClass+MyCategory.h //myClass+MyCategory.m

#import "myClass.h"
@interface myClass (MyCategory) -(void)workAt; @end

#import "myClass+MyCategory.h"
@implementation myClass (MyCategory) -(void)workAt{ NSLog(@"Develop at FPT"); } @end

Ch : - Tn ca category l duy nht. - Trong category khng cho php thm bin th hin.
LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Protocol trong Objective-C


Protocol nh ngha mt danh sch cc phng thc bt buc hoc tu chn m cc lp chp nhn. Bn thn protocol khng c s thc thi. Nu lp no cam kt thc thi cc phng thc trong protocol phi implement cc phng thc m protocol khai bo.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Protocol trong Objective-C (tt)


Khai bo protocol:
//MyProtocol.h @protocol MyProtocol // required (default) - (void) requiredMethod; @optional - (void) anOptionalMethod; - (void) anotherOptionalMethod; @required - (void) anotherRequiredMethod; @end
LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Protocol trong Objective-C (tt)


V d: ta c mt protocol biu din nhng g c th chy vi tn l Runnable
//Runnable.h @protocol Runnable -(void) run; @optional -(void) fly; @end

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Protocol trong Objective-C (tt)


Ta c lp Car v lp Air ci t protocol Runnable nh sau.

Lp Car:
//Car.h #import Runnable.h" @interface Car : NSObject <Runnable> -(void)run; @end //Car.m #import "Car.h" @implementation Car -(void)run{ NSLog(@"Xe hi chy trn ng b"); } @end

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Protocol trong Objective-C (tt)


Ta c lp Car v lp Air ci t protocol Runnable nh sau.

Lp Air:
//Air.h //Air.m #import "Air.h" @implementation Air -(void) run{ NSLog(@Air chy trn ng bng"); } -(void) fly{ NSLog(@Air bay trn bu tri"); } @end
LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

#import Runnable.h"
@interface Air : NSObject <Runnable> -(void) run; -(void) fly; @end

K tha
Tnh k tha ca Objective-C cng tng t cc ngn ng lp trnh khc (C#, Java). Trong Onjecive-C, root class ca tt c cc class l NSObject.
NSObject

NSString

NSArray

NSValue

NSMutableString

NSMutableArray

NSNumber

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

K tha (tt)
Trong NSObject c mt phng thc gi l init, cc lp con k tha s dng li phng thc init m bo cc i tng con c khi to ng n ging nhau. Trong qu trnh tha k, lp con c th ghi hay sa i ni dung hay c tnh ca lp cha -> method overiding.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

K tha (tt)
V d ta c m hnh: Parent class Person c cc property v name, address, birthday, method workAt.

Child class:
Developer k tha Person, ngoi ra cn xy dng thm cc property khc nh width, height, method info. Teacher ngoi vic k tha Person cn overriding method workAt, thm method info.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

K tha (tt)
Person
//Person.h
@interface Person : NSObject @implementation Person

//Person.m #import "Person.h"

@property (nonatomic) NSString *name; @property (nonatomic) NSString *address; @property (nonatomic) NSString *birthday; -(void) workAt;
@end

@synthesize name; @synthesize address; @synthesize birthday; -(void)workAt{ NSLog(@"Work at FPT"); } @end

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

K tha (tt)
Developer
//Developer.h #import "Person.h" @interface Developer : Person @property (nonatomic) int width; @property (nonatomic) int height;
//Developer.m #import "Developer.h" @implementation Developer @synthesize width; @synthesize height; -(void)info{ self.name = @"Bi Th Khim"; self.address = @"HCM"; self.width = 53; self.height = 160; NSLog(@"name = %@", self.name); NSLog(@"address = %@", self.address); NSLog(@"width = %i kg", self.width); NSLog(@"height = %i cm", self.height); } @end
LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

-(void) info;
@end

K tha (tt)
Teacher
//Teacher.m #import "Teacher.h" @implementation Teacher -(void)info{ self.name = @"Bi Th Kha"; self.address = @"HCM"; self.birthday = @"19/02/1990"; NSLog(@"name = %@", self.name); NSLog(@"address = %@", self.address); NSLog(@"birthday = %@", self.birthday); } -(void) workAt{ NSLog(@"Work at school"); } @end
LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

//Teacher.h #import "Person.h" @interface Teacher : Person -(void) info; -(void) workAt; @end

X l ngoi l
Cc t kho lin quan x l ngoi l:

@try nh ngha mt khi m lnh c th pht sinh ngoi l.


@throw nm ra mt ngoi l. @catch bt ngoi l bn trong khi m lnh @try trc n. @finally khi m lnh s c thc thi, khng cn bit ngoi l c xy ra hay khng.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Mt s cu trc iu khin
iu khin r nhnh: IF statement. WITCH statement. iu khin lp: FOR statement. WHILE statement.

Conditional operator.

DO statement.
BREAK statement.

CONTINUE statement

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh
Qun l b nh l qun l vic khi to vng nh cho ng dng, s dng v gii phng vng nh khi ta hon thnh cng vic.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
Cc phng php qun l b nh: Manual Reference Counting (MRC): Qun l b nh th cng. Automatic Reference Counting (ARC): Qun l b nh t ng ti thi im compile. Garbage Collection (GC): h thng t ng lu vt v gii phng vng nh.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
Cc khi nim v t kho cn lu :

Reference count (retainCount): l mt k thut qun l b nh ca objective-C nhm theo vt vng i ca i tng (object), m s lng tham chiu n mt i tng. Khi cc i tng c khi to bng t kho alloc, new, copy th retainCount t ng bng 1.
Tng retainCount ln 1 ta gi thng ip retain cho i tng.

Gim retainCount xung 1 ta gi thng ip release cho i tng.


Khi reference count gim xung 0 th i tng t hu, vng nh c gii phng, lc ny h thng s t ng gi thng ip dealloc cho i tng.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kho v v d:

alloc hoc new Cp pht b nh cho i tng. retainCount = 1;

MyClass *mcls1 = [[MyClass alloc] init]; MyClass *mcls2 = [MyClass new];

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kho v v d:

copy Cp pht b nh cho i tng thng qua mt i tng khc. retainCount = 1;


MyClass *mcls1 = [[MyClass alloc] init]; MyClass *mcls2 = [mcls1 copy];

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kho v v d:

copy Mun copy class phi overiding li phng thc copyWithZone trong Class.
//*.h

- (id) copyWithZone:(NSZone *)zone{ MyClass *myClass = [[MyClass allocWithZone: zone] init];


return myClass;

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kho v v d:

release Bo cho i tng bit ta s dng xong i tng v cn gii phng n. i tng s thc s gii phng ch khi khng cn ai s hu n na (retainCount = 0). retainCount = - 1;

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kho v v d:

retain Gi khi cn gi li i tng. Trong @property, retain ch nh hm setter gi li gi tr u vo. retainCount +1;

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kho v v d:

retain
NSArray *array1 = [[NSArray alloc] initWithArray:@[@"1", @"2", @"3"]]; NSArray *array2 = array1; [array1 release]; //Sau vi giy th array2 khng cn lu ng ni dung. Trong v d trn, bin array2 ch n gin l tr n vng nh ca bin array1. Mt khi bin array1 gii phng th bin array2 s lp tc bo li.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kho v v d:

retain
array1 Object in heap [1, 2, 3]

array2

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kho v v d:

retain

?
array2
LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kho v v d:

retain
Gii php: NSArray *array1 = [[NSArray alloc] initWithArray:@[@"1", @"2", @"3"]]; NSArray *array2 = [array1 retain]; [array1 release]; //array2 vn lu ng ni dung. [array2 release]; //cho n khi gii phng array2.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kho v v d:

retain
array1 Object in heap [1, 2, 3]

array2

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kho v v d:

retain

Object in heap [1, 2, 3]

array2

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kha v v d: autorelease. Dng khi no?

Khng mun gii phng i tng ngay lp tc.


Khng mun nh lc no nn release nhng i tng m ta to. Mun thm i tng vo autorelease pool.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kha v v d: autorelease. retainCount gim xung 1 nhng khng ngay lp tc.

Ch :
Ch c release khi khi to i tng th cng bng alloc. KHNG release th cng i tng autorelease.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kha v v d: autorelease.

NSString *str1 = [NSString stringWithFormat:@"Hello"];

autorelease

release

NSString *str2 = [[NSString alloc] init]; [str2 release];

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kha v v d: autorelease pool. L ni cha v gii phng cc autorelease object.

Khi autorelease pool c gii phng th cc object bn trong n cng s t ng gii phng.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kha v v d: autorelease pool. S dng khi no?

Khi ta mun t ng hu mt lot cc autorelease object.


Trong trng hp s dng nhiu bin tm v mun trnh cp pht v release cc bin ny khi s dng xong.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kha v v d: autorelease pool.
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSArray *array1 = @[@"1", @"2", @"3"]; NSArray *array2 = [[NSArray alloc] initWithArray:@[@"a", @"b", @"c"]]; //free menory [array2 release]; //free pool [pool release]; return 0;
LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kho v v d. dealloc c gi khi i tng ang c remove ra khi b nh.

Nu trong mt lp c cc bin th hin (instance variable) l cc i tng th trong phng thc dealloc phi thc hin gii phng cc bin th ny.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kho v v d.

dealloc
- (void) dealloc{ [childVar1 release]; [childVar2 release]; //Do some thing; [super dealloc]; }
Phng thc [super dealloc] s dng thng bo cho lp thc hin vic dn dp. [super dealloc] l phng thc c nh ngha trong lp mc nh NSObject.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
T kho v v d. Dealloc S dng dealloc khi no?

Thng c s dng khi gii phng tt c cc tham chiu n bin th hin con ca i tng khi b nh.
iu g xy ra khi khng s dng dealloc? i tng s khng c remove ra khi b nh => gy ra tnh trng chim dng b nh.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
Tng kt: Khi no mi tht s s hu i tng? Khi ta cp pht b nh cho cc i tng bng t kho alloc, new, copy, retain.

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

Qun l b nh (tt)
Tng kt: Quy lut c bn: S hu i tng no th mi c release i tng .

Khng s hu th khng release.


Override li hm dealloc trong lp release cc fileds s hu. Khng gi dealloc trc tip. Nn s dng autorelease ngay sau khi khi to v cp pht b nh.
LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

LP TRNH NG DNG TRN iOS Phn 1: Gii thiu objective-C KHIEMBT@FPT.COM.VN

You might also like