Professional Documents
Culture Documents
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.
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.
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;
NSString *stringURL;
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.
#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
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 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
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.
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.
K tha (tt)
Person
//Person.h
@interface Person : NSObject @implementation Person
@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
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:
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
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.
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.
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.
Qun l b nh (tt)
T kho v v d:
Qun l b nh (tt)
T kho v v d:
Qun l b nh (tt)
T kho v v d:
copy Mun copy class phi overiding li phng thc copyWithZone trong Class.
//*.h
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;
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;
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.
Qun l b nh (tt)
T kho v v d:
retain
array1 Object in heap [1, 2, 3]
array2
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.
Qun l b nh (tt)
T kho v v d:
retain
array1 Object in heap [1, 2, 3]
array2
Qun l b nh (tt)
T kho v v d:
retain
array2
Qun l b nh (tt)
T kha v v d: autorelease. Dng khi no?
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.
Qun l b nh (tt)
T kha v v d: autorelease.
autorelease
release
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.
Qun l b nh (tt)
T kha v v d: autorelease pool. S dng khi no?
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.
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.
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.
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.
Qun l b nh (tt)
Tng kt: Quy lut c bn: S hu i tng no th mi c release i tng .