intro-to-objc2-0-pptx-100224025204-phpapp01

  • Upload
    almase

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    1/94

    Introduc)ontoObjec)ve-C

    JussiPohjolainen

    TampereUniversityofAppliedSciences

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    2/94

    QUICKSTART

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    3/94

    Background

    Objec)ve-CislayeredontopoftheClanguage

    BasedonSmallTalk-80Designedinearly1980s

    NeXTSoLwarelicensedObjec)ve-Cin1988 AppleComputeracquiredNeXTin1996 Today:na)velanguagefordevelopingMacOSX-andiPhone-apps

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    4/94

    ClassDeclara)on:MyPoint.h

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    5/94

    ClassImplementa)on:MyPoint.m

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    6/94

    TesttheClass:main.m

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    7/94

    Simplemakefile

    MyPoint : MyPoint.m main.m

    clang -fno-objc-arc -framework foundation

    MyPoint.m main.m -o MyPoint

    run :

    ./MyPoint

    clean :

    rm MyPoint

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    8/94

    CompilingandRunning

    TB308POHJUS-L-2:point pohjus$ ls -altotal 32

    drwxr-xr-x 6 pohjus staff 204 18 Tou 15:32 .

    drwxr-xr-x 3 pohjus staff 102 18 Tou 14:52 ..

    -rw-r--r--@ 1 pohjus staff 196 18 Tou 15:25 MyPoint.h

    -rw-r--r--@ 1 pohjus staff 268 18 Tou 15:23 MyPoint.m

    -rw-r--r--@ 1 pohjus staff 339 18 Tou 15:25 main.m

    -rw-r--r--@ 1 pohjus staff 120 18 Tou 15:14 makefile

    TB308POHJUS-L-2:point pohjus$ makeclang -fno-objc-arc -framework foundation MyPoint.m main.m -o MyPoint

    TB308POHJUS-L-2:point pohjus$ make run./MyPoint

    2009-05-18 15:32:46.339 MyPoint[8725:807] X = 0 and Y = 02009-05-18 15:32:46.341 MyPoint[8725:807] X = 8 and Y = 7

    TB308POHJUS-L-2:point pohjus$

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    9/94

    Instan)a)nganObject

    // Declare a pointer to the object

    MyPoint* point;

    // Allocate memory for the objectpoint = [MyPoint alloc];

    // Initialize the object

    point = [point init];

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    10/94

    Instan)a)nganObject:Oneiner

    // Allocate and initialize the object

    MyPoint* point1 = [[MyPoint alloc] init];

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    11/94

    Messages(Methods

    - (void) setX: (int) n;!

    methodtype:

    +=classmethod

    -=objectmethod

    returntype selectorname

    argumenttype

    argument

    name

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    12/94

    Messages,withTwoArguments

    Declara=on

    - (void) setXAndY: (int) x: (int) y

    Usage

    [object setXAndY: 5: 6];

    Declara=on,be@erway

    - (void) setX: (int) x andY: (int) yUsage

    [object setX: 5 andY: 6];

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    13/94

    EXERCISES

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    14/94

    MEMORYMANAGEMENTANDOBJECT

    LIFECYCLE

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    15/94

    MemoryHandlinginGeneral

    WhenallocatesomethingitmustbereleasedMemoryconsump)on

    InJava,garbagecollectortakescareoftherelease.Separatethreadlookingforobjectsthatcanbereleased

    InObj-CandC/C++,programmerisresponsibleabouttherelease.

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    16/94

    AboutPointers

    inta5;HoldsoneintegervalueTheintegerisstoredinsomememoryaddress

    Where?Youcangetthememoryaddressbyusing&infrontofthevariable:&a

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    17/94

    ExampleofMemoryAddress

    int main(int argc, char *argv[])

    {

    int a = 5;

    // prints 5

    NSLog(@"%i", a);// prints something like 0x7fff5fbff9cc

    NSLog(@"%p", &a);return 0;

    }

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    18/94

    Pointers

    Pointerisavariablethatstoresmemoryaddress

    inta;holdsintegervariable

    int*b;holdsmemoryaddressthatpointstointegervalue

    int*b&a;Nowbhasthememoryaddressofa

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    19/94

    ExampleaboutPointers

    #import "MyPoint.h"

    int main(int argc, char *argv[])

    {

    int a = 5;

    // Store a's memory address to variable b

    int *b = &a;

    // prints 5

    NSLog(@"%i", a);

    // prints something like 0x7fff5fbff9cc

    NSLog(@"%p", &a);

    // prints something like 0x7fff5fbff9cc

    NSLog(@"%p", b);

    // prints 5

    NSLog(@"%i", *b);

    return 0;

    }

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    20/94

    Whatistheresultnow?

    int main(int argc, char *argv[])

    {

    int a = 5;

    // Store a's memory address to variable b

    int *b = &a;

    if(b == &a)

    {

    NSLog(@"Do we go here?");

    }

    if(*b == a)

    {

    NSLog(@"What about here?");

    }

    return 0;

    }

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    21/94

    Whatistheresultnow?

    int main(int argc, char *argv[])

    {

    int a = 5;

    int *b = &a;

    *b = 10;

    NSLog(@"%i", a);

    return 0;}

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    22/94

    MemoryAreas

    Memorycanbedividedintothreecategories1. Globalorsta)c2. Stack3. Heap

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    23/94

    Sta)c

    Whensomethingisinsta)cmemory,it'sthereallthe)mewhenappisrunning

    Sowhenstar)ngtheapp,thememoryisallocatedanwhentheappcloses,thememoryisdeallocated

    Thevariableisstoredinthesamememoryaddressallthe)me.

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    24/94

    ExampleofSta)cMemory

    int thisIsStoredInStaticMemory = 5;

    int main(int argc, char *argv[])

    {

    static int thisIsAlsoStoredInStaticMemory = 10;

    NSLog(@"My number = %i", thisIsStoredInStaticMemory);

    NSLog(@"My number = %i", thisIsAlsoStoredInStaticMemory);

    return 0;

    }

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    25/94

    Stack-Memory

    Stackmemoryareaisusuallysmall Ifyouputtoomuch"stuff"intostack,youmightgetstackoverflow

    ocalvariablesarestoredinstack! Variablesarereleasedwhenoutofscope

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    26/94

    ExampleofStackMemory

    int main(int argc, char *argv[])

    {

    int stack1 = 3;

    NSLog(@"My number = %i", stack1);

    if(YES){

    int stack2 = 4;

    NSLog(@"My number = %i", stack2);

    // stack2 is released from memory now.}

    // stack1 is released from memory now.return 0;

    }

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    27/94

    ExampleofStackMemory

    int main(int argc, char *argv[])

    {

    if(YES)

    {

    int stack2 = 4;}

    // Does this work?

    NSLog(@"My number = %i", stack2);

    return 0;}

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    28/94

    Heap-Memory

    Heapmemoryisthelargememoryareawherealmostalloftheobjectsgo.

    Programmerisresponsibleforreleasingtheobjects!

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    29/94

    ExampleofHeap-memory

    #import "MyPoint.h"

    int main(int argc, char *argv[])

    {

    MyPoint* point = [MyPoint alloc];//...

    [point release];

    return 0;

    }

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    30/94

    Crea)ngaObject

    Thecrea)onofanobjectisdoneintwoparts 1Alloca)ngmemory

    MyPoint* point = [MyPoint alloc];2Ini)alizeobjectstatepoint = [point init];

    CombinedMyPoint* point = [[MyPoint alloc] init];

    MyPoint* point = [MyPoint new];

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    31/94

    WhathappensinMemory?

    Whathappensinhere?MyPoint* p = [MyPoint alloc];

    Twothings!MyPoint* p;p = [MyPoint alloc];

    pisinstackmemory!MyPointobjectisinheapmemory!

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    32/94

    Problem?

    #import "MyPoint.h"

    int main(int argc, char *argv[])

    {

    if(YES)

    {MyPoint* point = [MyPoint alloc];

    }

    [point release];

    return 0;

    }

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    33/94

    Problem?

    #import "MyPoint.h"

    int main(int argc, char *argv[])

    {

    MyPoint* point = [MyPoint alloc];point = [MyPoint alloc];

    [point release];

    return 0;}

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    34/94

    init-method?

    init-methodisimplementedinNSObject Youcanhoweverimplementyourowninit-method

    - (id) initWithName: (NSString*) aName{

    if(self = [super init])

    {

    name = aName;

    }

    return self;

    }

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    35/94

    Otherinit-methods

    ikeinJavaandC++,onecanhavemul)pleconstructors

    InObj-C,onecanhavemul)pleinit-methods

    - (id) init- (id) initWithX: (int) aX;- (id) initWithX: (int) aX andY: (int) aY

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    36/94

    DeallocingObjectandReferenceCount

    Rulesaboutreferencecoun=ng Whenobjectiscreateditsreferencecountissetto1 Incremen)ngthereferencecount:

    [point retain]; Decreasingreferencecount

    [point release]; Whenreferencecountreachesto0,dealloca=ngoccurs

    ARCwillsaveyoufromthis!

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    37/94

    ReferenceCountExample

    #import

    @interface Cat : NSObject

    {

    }

    - (void) printMyRetainCount;

    @end

    @implementation Cat

    - (void) printMyRetainCount

    {NSLog(@"Retain count = %i", [self retainCount]);

    }

    @end

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    38/94

    ReferenceCountExample

    int main(int argc, char *argv[])

    {

    Cat* myCat = [[Cat alloc] init];

    [myCat printMyRetainCount]; // Retain count = 1

    Cat* reference = myCat;

    [reference retain];

    [myCat printMyRetainCount]; // Retain count = 2

    [myCat release];

    [myCat printMyRetainCount]; // Retain count = 1

    [myCat release]; // Deallocation

    return 0;

    }

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    39/94

    dealloc

    Youcanimplementadeallocmethod(void) dealloc

    {

    // Some code

    [super dealloc];

    }

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    40/94

    ReferenceCountExample

    #import

    @interface Cat : NSObject

    {

    NSString* name;

    }

    - (void) setName: (NSString *) theName

    @end

    @implementation Cat

    - (void) setName: (NSString *) theName

    {name = theName;

    }

    @end

    int main(int argc, char *argv[])

    {

    ....

    Cat* mirri = [[Cat alloc] init];

    [mirri setName: someName];

    // What happens now?[someName release];

    }

    Sincereferencecountis

    0,dealloca)onoccurs.

    Thismeans,thattheCat

    doesnothaveaname

    anymore.

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    41/94

    ReferenceCountExample

    #import

    @interface Cat : NSObject

    {

    NSString* name;

    }

    - (void) setName: (NSString *) theName

    @end

    @implementation Cat

    - (void) setName: (NSString *) theName

    { [name release];name = theName;

    [name retain];}

    @end

    int main(int argc, char *argv[])

    {

    ....

    Cat* mirri = [[Cat alloc] init];

    [mirri setName: someName];

    // What happens now?[someName release];

    }

    Sincereferencecountis

    1,dealloca)ondoesnot

    occurandtheCats)ll

    hasit'sname.

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    42/94

    CopyingaObject

    - (void)setName:(NSString *)theName

    }

    [name release];

    name = [theName copy];}

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    43/94

    AutoreleasePool

    EveryFounda)onprogrammustsetupautoreleasepoolfortheFounda)onobjects

    Poolkeepstrackofyourobjectsforlaterrelease NSAutoreleasePool *pool = [[NSAutoreleasePoolalloc] init];

    ... [pool drain];

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    44/94

    MethodNames

    Ifmethodnameincludesallocorcopy,itreturnsaobjectthatmustbereleased // Must be released NSObject* object = [[NSObject alloc] init]; // Must be released NSObject* copy = [object copy]; // Do not have to release NSMutableString* string = [NSMutableString string];

    TheabovestringisreleasedinAutoreleasePool!

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    45/94

    Problem

    // Programmer A code

    [[someObject giveCat] eat];

    // Programmer B code

    - (Cat*) giveCat

    {

    // Must be released!

    Cat* myCat = [[Cat alloc] init];

    // But where? Should the programmer who calls this method be

    // responsible for deallocation of the Cat? How does the programmer

    // know this?return myCat;

    }

    ..

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    46/94

    Solu)on

    // Programmer A code

    [[someObject giveCat] eat];

    // Programmer B code

    - (Cat*) giveCat

    {

    // Must be released!

    Cat* myCat = [[Cat alloc] init];

    // But where? When autopool is drained!

    [myCat autorelease];return myCat;

    }

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    47/94

    DelayedRelease

    Autoreleasemeans"sendreleasemessagelater".

    ReleasemessageissentwhenAutoreleasePoolisreleased

    AutoreleasePooliscreatedandreleasedinUIKitprogramsautoma=cally!

    PooliscreatedatthebeginningofaneventcyclePoolisreleasedattheendofaneventcycle

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    48/94

    AutoreleasePools

    Appoaded WaitforEvent Handleevent Exitapp

    Eventloop

    Pool

    created

    Pool

    released

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    49/94

    AutoreleasePoolUsage

    // Method

    - (Cat*) giveCat

    {

    // Must be released!

    Cat* myCat = [[Cat alloc] init];

    // But where? When autopool is drained!

    [myCat autorelease];return myCat;

    }

    // Usage

    Cat* someCat = [object giveCat];

    // someCat will be released in some time, so if you want to hold it, use

    // retain

    [someCat retain];

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    50/94

    Rules

    Ifmethodnamecontains"alloc","new"or"copy",youmustremembertouserelease

    orautorelease

    Example:alloc, newObject, mutableCopy Ifyouretain something,youmustuserelease orautorelease

    InstanceVariables:retainorcopy autoreleasemeans"sendrelease later"

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    51/94

    Cat.h

    #import

    @interface Cat : NSObject

    {

    @privateNSString* name;

    }

    - (id) initWithName: (NSString*) aName;

    - (void) setName: (NSString*) aName;

    - (NSString*) getName;

    - (void) dealloc;

    @end

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    52/94

    Cat.m

    #import "Cat.h"

    @implementation Cat

    - (id) initWithName: (NSString*) aName

    {

    if(self = [super init])

    {

    [self setName: aName];}

    return self;

    }

    - (NSString*) getName

    {

    return name;

    }

    - (void) setName: (NSString*) aName

    {

    if(aName != name)

    {

    [name release];

    name = aName;

    [name retain];

    }

    }

    - (void) dealloc

    {

    [name release];

    [super dealloc];

    }

    @end

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    53/94

    main.m

    #import "Cat.h"

    int main(int argc, char *argv[])

    {

    NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];

    // Create the string

    NSString* catName = [[NSStringalloc] initWithString: @"Jack"];

    // Create cat with the string

    Cat* cat = [[Cat alloc]initWithName: catName];

    // Just testing. This does notdeallocate catName!

    [catName release];

    // Get the name

    NSString* name = [cat getName];

    // Print the name

    NSLog(name);

    // Release name and cat

    [cat release];

    [pool drain];

    return 0;

    }

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    54/94

    MANAGINGMEMORYWITHARC

    ARCtotherescue!

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    55/94

    ARC?

    ARC(Automa=cReferenceCoun=ng)Compilerdoesautoma/creferencecoun/ngbyexaminingthesourcecodeandthenaddtheretainandreleasemessagestoobjects

    Notgarbagecollec)on,nobackgroundprocessofdealloca)onofobjects!

    Insertsretainandreleasestatementsbasedonsomefixedrules

    OSX10.7andiOS5forallfeatures

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    56/94

    Objectloseowners

    // Scenario 1

    Person* jack = [[Person alloc] init];

    jack = [[Person alloc] init];

    // Scenario 2

    Person* tina = [[Person alloc] init];

    tina = nil;

    // Scenario 3

    if(yes) {

    Person* dave = [[Person alloc]init];}

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    57/94

    SomeFixedrules

    Ifobjectisallocatedandlocaltomethod,releasestatementisaddedneartheendof

    thatmethod

    Ifallocatedobjectisclassa@ribute,releaseisaddedtodealloc Iftheobjectisreturnvalue,itgetsanautoreleasestatement

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    58/94

    Guidelines

    Dontcall!retain, release, retainCount, autoreleaseor dealloc

    Youmustuseautoreleasepoolsyntax YoumustenableARC

    clang -fobjc-arc -framework foundationCar.m Motor.m main.m -o App

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    59/94

    makefile

    MyPoint : Car.m Motor.m main.m

    clang -fobjc-arc -framework foundation Car.m Motor.m main.m

    -o App

    run :

    ./App

    clean :

    rm App

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    60/94

    main.m

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    61/94

    motor.h

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    62/94

    motor.m

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    63/94

    car.h

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    64/94

    car.m

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    65/94

    PROPERTIES

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    66/94

    Objec)ve-C2.0:@property

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    67/94

    Objec)ve-C2.0:@synthesize

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    68/94

    Objec)ve-C2.0:DotSyntax

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    69/94

    Autosynthesize

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    70/94

    Autosynthesize

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    71/94

    PropertyDeclara)onAributes:

    Writability

    Youcandecorateapropertywithaributes,example:

    @property (readonly) int x; readwrite

    Indicatesthatthepropertyisread/write.Default readonly

    OnlyreadGeneratesonlygeermethod

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    72/94

    SeerSeman)cs

    assignSimpleseer.Default.

    weakNon-owningrela)onshipwithanobject Ifobjectisdeallocated,thepropertyissettonil

    strongOwningrela)onshipwithanobject

    copySpecifiesthatacopyoftheobjectshouldbeusedforassignment

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    73/94

    SeerSeman)csExamples

    // assignproperty = newValue;

    // copyif (property != newValue)

    {

    [property release];

    property = [newValue copy];

    }

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    74/94

    Atomicity

    nonatomicSpecifiesthataccessorarenon-atomic.

    Proper)esareatomicbydefault:[_internal lock];id result = [[value retain] autorelease];[_internal unlock];return id;

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    75/94

    Car.h

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    76/94

    Motor.h

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    77/94

    Main.m

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    78/94

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    79/94

    Change!Car.h

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    80/94

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    81/94

    STRINGS

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    82/94

    AboutStrings

    CStringchar * // Array of characters

    NSStringObject,thatholdsarrayofUnicodecharactersIsimmutable,contentscannotbechangedaLerwards!

    NSMutableStringStringthatcanbemodifiedaLerwards

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    83/94

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    84/94

    Formang

    Formang NSString *string1 = [NSString stringWithFormat:@"A

    string: %@, a float: %1.2f",@"string", 31415.9265]; // string1 is "A string: string, a float: 31415.93"

    FormatSpecifiers?http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/

    Strings/Articles/formatSpecifiers.html#//

    apple_ref/doc/uid/TP40004265-SW1

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    85/94

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    86/94

    NSMutableString methods

    NSMutableString inheritesNSString WithNSMutableString youcanmodifythestringwiththesemethods

    appendFormat:appendString:deleteCharactersInRange:insertString:atIndex:replaceCharactersInRange:withString:replaceOccurrencesOfString:withString:options:range:setString:

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    87/94

    PROTOCOLS

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    88/94

    Protocols?

    ComparedtoJava,protocolsareinterfaces Youdefinemethodsthatsomeobjectmustimplement

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    89/94

    UsingProtocols

    // MyProtocolName.h

    // Notice that the protocol inherites NSObject

    // protocol!

    @protocol MyProtocolName //Method declarations go here@end

    // MyObject

    @interface Class: NSObject

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    90/94

    ProtocolasVariable

    InJavaMyInterface object = new MyObject();

    InObj-Cid object = [[MyObjectalloc] init];

    Asamethodargument(void) doSomethingWithThisObject:(id) aObject

    IDisapredefinedpointertypeforanarbitraryobject

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    91/94

    FOUNDATIONCLASSES

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    92/94

    NSObject

    NSObjectistherootclassofMostObj-Cclasses

    Crea)ng,copying,dealloca)ngobjects

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    93/94

    Collec)ons

    Array:OrderedCollec/ons Dic)onary:Collec/onsofKeysandValues Set:UnorderedCollec/onsofObjects CountedSets:UnorderedCollec/onofIndis/nctObjects

    Enumera)on:TraversingaCollec/on'sElements

    Mutableandimmutableversions!

  • 7/29/2019 intro-to-objc2-0-pptx-100224025204-phpapp01

    94/94

    OtherClasses

    NSNumber,wrapperforstandardnumbertypes

    NSDate,NSCalendarDate