ios programing

Embed Size (px)

Citation preview

  • 7/29/2019 ios programing

    1/21

    Learn Objective CNguyn Giang [email protected]

    092.881.3468

  • 7/29/2019 ios programing

    2/21

    Ngn ng lp trnh Objective C (ObjC) l mt ngn ng lp trnh da trn nn tng ngn ng C. Mi th lm vic trong C u lm vicObjC. ObjC thm vo nhiu tnh nng mi t C, thm lp trnh tng (Object-Oriented Programming, OOP) v Smalltalk message

    Objective C l ngn ngc bin dch thnh m my (native maging vi C/C++, v do n nhanh hn so vi nhng ngn ng cho nh Java, Python hoc Ruby.

    Mt khc, ObjC c nhiu tnh nng lm cho n linh ng hn C++. Obnh kiu tnh static typing v nh kiu ng dynamic typing, tubin c khai bo. Nhng v hiu nng lm vic th ObjC km hn C

    Ngn ng Objective C c ra mt vo nm 1983 bi Brad Cox v To

    Gii thiu

  • 7/29/2019 ios programing

    3/21

    Trong Objective C khng s dng namespaces ging nh cc ngnthay vo phn bit cc class n t cc frameworks khc nhdng name prefixes tn ca class.

    Cc tp tin c t chc bng cch tng t nh vi C/C++: tp tin ticha cc khai bo, tp tin m ngun (*.m) - thc hin vit cc dnkhai. (*.h) ngha l header, (*m) ngha l message hoc l methods

    Chng trnh thc thi bt u vi hm int main(). c th s dng frameworks hoc cc class tnh ngha cn s d

    #include.

    Source Code

  • 7/29/2019 ios programing

    4/21

    Chng c v ging nhau, nhng khng phi l mt. Class l mttrong khi Object l mt th hin ca Class to. Class nh mt bntrong khi Objects l i tng tn ti thc s.

    C php to ra mt class trong Objective C l rt n gin. N bphn:- Class interface thng c lu tr trong tp tin ClassName.h, ninstance variables v public method.

    - M ngun thc thi c vit trong tp tin ClassName.m. Objective C s dng n k tha. Sau y l c php thng dng to ra class:

    Class & Object

  • 7/29/2019 ios programing

    5/21

    Class Header (.h)

    #import "AnyHeaderFile.h"@interface ClassName : SuperClass {

    // declare instance variables

    }

    // define properties

    // define methods (including any // custom initializers)

    @end

    Class Implementation (.m)

    #import "YourClassName.h

    @implementation ClassName

    // synthesize properties

    // implement methods (including any custom initializers, and dealloc)

    @end

    Class & Object

  • 7/29/2019 ios programing

    6/21

    Cch khi to Object (Class instance)://cch th nht:ClassName * myClass = [[ClassNamealloc] init];

    //cch th hai:ClassName * myClass = [ClassNamenew];

    //cch th ba:ClassName * myClass = [[ClassNamealloc] initWithSomething:someth

    //myClass c s dng cho n khi gi release:[myClass release]; // khng cn thit vi ARC

    Class & Object

  • 7/29/2019 ios programing

    7/21

    Variable access (quyn truy cp) c thit lp bi cc t kho@private, @protected; cc t kho s dng c t trc nhm vthit lp. Thit lp mc nh l @protected.

    truy cp instance variable s dng -> v d: person->name . Nu bn mun s dng static variable trong class, khi khai bo bn

    kho static trong header file, bn ngoi vng ca @interface (vanytype staticVariable;).

    Cch khai bo variables: anytype myVariable;@public

    anytypeA myVariableA;

    anytypeB myVariableB;

    Variable & PropertVariable

  • 7/29/2019 ios programing

    8/21

    Trong @interface, khai bo nh sau @property anytype age;. Tngvic khai bo getter: (anytype) age; (trong ObjC getter khng c tin setter: (void) setAge: (anytype)age.

    Trong @implementation, s dng @synthesize age;, tng ngcc lnh thc thi getter v setter mc nh. C th to ra cc lnh thccho getter v setter bng vic overide li setter v getter m khn@synthesize.

    trnh nhm ln gia variable v property, cc property khng th s truy cp m s dng obj.age hoc [obj age].

    Cc property c th c cc thuc tnh tu chn bng cch thm vo s@property, nh l: @property (readonly) anytype age; C th tm hi

    thuc tnh t ti liu apple cung cp.

    Variable & PropertProperty

  • 7/29/2019 ios programing

    9/21

    Cch nh ngha mt method:- (void)doIt; hoc - (anytype)doIt;+ (void)doIt; hoc + (anytype)doIt; //class method- (anytype)doItWithA:(anytype)a;

    - (anytype)doItWithA:(anytype)a andB:(anytype)b;

    Cch trin khai method:- (anytype)doItWithA:(anytype)a andB:(anytype)b {

    // Do something with a and b... return retVal;

    }

    Method

  • 7/29/2019 ios programing

    10/21

    Mt iu khc bit v method trong Objective C l cc tham sc vi khi nim label, c s dng trong vic gi hm (vi Objectivehm c nh ngha l gi mt message ti object k tha t Sma

    Label thc sc hnh thnh t tn y ca method. Bi vy mc label l doItWithA:andB:.

    Method trong Objective C khng th overload bng kiu d liu ca thtrong cc ngn ng thng dng, nn khng th cng lc c

    doIt(typeA) v doIt(typeB). Trong Objective C, khi gi mt message (gi mt method) khng tn t

    a ra li lc bin dch, nhng sa ra exception trong lc thc thiv x l vi c php try...catch.

    Method i khi c s dng vi khi nim l selector.

    Method

  • 7/29/2019 ios programing

    11/21

    Trong ObjC, ngoi vic k tha t cc class, bn cng c th thm coclass hin c. C th pht trin cc class hin c m khng cn pcode trong class , tuy n khng mnh m nh Ruby (v d nh bthm cc instance varibale mi) nhng iu ny thc s rt tuyt vi.

    y n c gi l category bi v nu c mt class nhiu metphn chia cc method ca n thnh nhiu loi v t chng trong cnhau.

    Di y l cch khai bo mt category:@interface ExistingClass (SomeNewName)// methods

    @end

    Sau , include c header c v header mi vo ni bn mun s d c mt class vi cc method mi c thm vo.

    Category

  • 7/29/2019 ios programing

    12/21

    ObjC c c hai kiu d liu l primitive v object. Vi kiu object chtr v khng c kiu gi tr.

    Cc kiu d liu primitive (c bn): char, int, float, double, short, loObjC khng khuyn khch s dng cc kiu d liu primitive, c cc kv d nhNSInteger, NSUInteger, hoc CGFloat (y l cc phin trc an ton ca int, unsigned int, float). Ngoi ra cn c mt class ls dng cho vic boxing/unboxing kiu pimitive khi cn chuyn thnh

    object. Kiu d liu logic c gi l BOOL, v gi tr khng ging nh c

    khc True/False m ObjC s dng YES/NOJ.

    Kiu Null gi l nil. i vi kiu con tr, nil c vit l Nil. TNSNumbercho int, class dng cho nil l NSNull.

    Kiu chui trong ObjC l NSString.

    Data types

  • 7/29/2019 ios programing

    13/21

    Cu trc ca lnh vng lp trong ObjC khng khc bit nhiu vi ckhc. ObjC c cc lnh lp for, while, do-while, for-each.

    Sau y l c php ca cc vng lp:

    for(; ; ) {

    }

    for(int i=0; i

  • 7/29/2019 ios programing

    14/21

    while () {

    }

    do {

    } while ();

    for( in ) {

    }

    Loopswhile (a

  • 7/29/2019 ios programing

    15/21

    ObjC c cc lnh iu kin: if, switch.

    Sau y l c php ca cc lnh iu kin:

    Conditions

    if() {

    }

    switch () {

    case :

    break;

    default:

    break;

    }

    if(a

  • 7/29/2019 ios programing

    16/21

    Protocol nh ngha mt danh sch cc method bt buc hoc ty cclass chp nhn (adopt) protocol phi thc thi.

    Cc method khai bo trong protocol cng c th l cc khai bo propkha @optional v @required th hin theo ng ngha ca n, ndng t kha th mc nh l @required.

    Protocol c s dng trong cc trng hp:- Khai bo cc method d kin sc thc thi.

    - Khai bo mt interface cho mt object trong khi n i class ca n.- Kho st tng ng gia cc class m khng phi lin quan n cbc.

    Protocols

  • 7/29/2019 ios programing

    17/21

    Cch khai bo mt protocol@protocol FooInterface

    - (int) foo;

    - (void) bar;

    @end

    Cch class chp nhn (adopt) protocol@interface MyClass: NSObject

    Cc method khai bo trong protocol khng cn thit phi khai bo li tr@interface ca class.

    Protocols

  • 7/29/2019 ios programing

    18/21

    ObjC c kh nng ng hn C v C++, mc d thc t rng n thngn ng bin dch trc tip m my mnh m. Di y l mt s vchc nng lin quan n reflection:

    o isKindOfClass:[MyClass class] - cho bit rng object c class MyClass hay khng.

    o isMemberOfClass:[MyClass class] - cho bt rng class ca objecMyClass hay khng.

    o respondsToSelector:@selector(doIt) - cho bit rng object c methkhng.

    o performSelector:@selector(doIt) - gi method c chnh doIt: c

    Reflection

  • 7/29/2019 ios programing

    19/21

    Exceptions lm vic ging nh trong tt c cc ngn ng hin i kc i din bi cc class k tha (thng thng) tNSException, cc t kho @try, @catch v @finally chn cc exception ri nm mt block (khi) v x l chng. S dng @throw e nm ra exceptio

    Hai method c th hu ch trong debug (g li):o NSAssert(x != 0, @"X must not be zero"); - a ra exception nu x o NSLog(@result: x = %@", x); - ghi ra mn hnh console chui, v

    mt vi dng vo /var/log/system.log.

    Exceptions & debugg

  • 7/29/2019 ios programing

    20/21

    Cc object trong ObjC l reference counted(tham chiu m).o

    Cc object bt u vi reference l 1.o Tng reference vi method retain.o Gim reference vi method release, autorelease.o Khi s lng reference l 0, runtime (trnh bin dch) s gi method

    Khi bn nm quyn s hu mt object, khi to object bng cc methtn bt u vi alloc, new hoc copy (v d, alloc, allocWithZone

    hoc mutableCopy) hoc gi mt message retain, bn phi c trchphng quyn s hu object bng cch s dng release hoc autok khi no bn nhn c mt object (khng phi t mnh khi to)c release n.

    T iOS 4.3 v sau h tr qun l b nh tng (ARC) vi garbagARC thc s gip developer d dng loi b memory leaks(tht thot

    Memory manageme

  • 7/29/2019 ios programing

    21/21

    @implementationBankAccount

    - (NSString *)accountNumber {

    return [[accountNumberretain] autorelease];}

    - (void)SetAccountNumber {

    if(accountNumber != newNumber) {

    [accountNumberrelease];

    accountNumber = [newNumberretain];

    }}

    - (void)dealloc {

    [selfsetAccountNumber:nil];

    [superdealloc];

    }

    @end

    retain/release/autorel