+ All Categories
Home > Documents > Week 7 - Objective C - Tuesday

Week 7 - Objective C - Tuesday

Date post: 30-May-2018
Category:
Upload: william-smith
View: 219 times
Download: 0 times
Share this document with a friend

of 23

Transcript
  • 8/14/2019 Week 7 - Objective C - Tuesday

    1/23

    September 29, 20

    Issues

  • 8/14/2019 Week 7 - Objective C - Tuesday

    2/23

    September 29, 20

  • 8/14/2019 Week 7 - Objective C - Tuesday

    3/23

    September 29, 20

  • 8/14/2019 Week 7 - Objective C - Tuesday

    4/23

  • 8/14/2019 Week 7 - Objective C - Tuesday

    5/23

    September 29, 20

  • 8/14/2019 Week 7 - Objective C - Tuesday

    6/23

    September 29, 20

  • 8/14/2019 Week 7 - Objective C - Tuesday

    7/23

    September 29, 20

  • 8/14/2019 Week 7 - Objective C - Tuesday

    8/23

    September 29, 20

  • 8/14/2019 Week 7 - Objective C - Tuesday

    9/23

    September 29, 20

    Protocols

  • 8/14/2019 Week 7 - Objective C - Tuesday

    10/23

  • 8/14/2019 Week 7 - Objective C - Tuesday

    11/23

    September 29, 20

    Aprotocol,inObjectiveC,isalistofmethoddeclarationsthatanyclassthatwishestoadopttheprotocolmust

    implement.

    Wewriteaprotocolinasimilarwaytowritingan@interfacedeclaration.Hereisonetypicalprotocol.

    @protocolDocument(int)wordCount;(id)title:(char*)new_title;

    (char*)title;@end

  • 8/14/2019 Week 7 - Objective C - Tuesday

    12/23

  • 8/14/2019 Week 7 - Objective C - Tuesday

    13/23

    September 29, 20

    The protocol specification is quite simple. it is basically@protocol ProtocolName(methods you must implement)@end.To conform to a protocol, you put the protocols you're conforming toin 's, and comma separate them.Example: @interface SomeClass

  • 8/14/2019 Week 7 - Objective C - Tuesday

    14/23

    September 29, 20

    CodeTime!

  • 8/14/2019 Week 7 - Objective C - Tuesday

    15/23

  • 8/14/2019 Week 7 - Objective C - Tuesday

    16/23

    September 29, 20

    OneuniqueaspectofObjectiveC'sinterfacesystemishowyouspecifytypes.RatherthanspecifyingitlikeJavaorC++as:Printing*someVar=(Printing*)frac;forexample,youusetheidtypewitharestrictedprotocol:idvar=frac;Thisallowsyoutodynamicallyspecifyatypethatrequiresmultipleprotocols,allwithonevariable.

    Suchas:idvar=frac;

    Muchlikeusing@selectorfortestinganobject'sinheritance,youcanuse@protocoltotestforconformanceofinterfaces.[objectconformsToProtocol:@protocol(SomeProtocol)]returnsaBOOLiftheobjectconformstothatprotocol.

    Thisworksthesameforclassesaswell:[SomeClassconformsToProtocol:@protocol(SomeProtocol)].

  • 8/14/2019 Week 7 - Objective C - Tuesday

    17/23

    September 29, 20

    Categories

  • 8/14/2019 Week 7 - Objective C - Tuesday

    18/23

    September 29, 20

    Whenyouwanttoaddmethodstoaclass,

    youtypicallyextendit.Howeverthis

    solutionisn'talwaysperfect,especiallyifyouwanttorewritethefunctionalityofa

    classthatyoudon'thavethesourcecode

    to.Categoriesallowyoutoadd

    functionalitytoalreadyexistingclasses

    withoutextendingthem.

  • 8/14/2019 Week 7 - Objective C - Tuesday

    19/23

  • 8/14/2019 Week 7 - Objective C - Tuesday

    20/23

    September 29, 20

    Code Time!

  • 8/14/2019 Week 7 - Objective C - Tuesday

    21/23

    September 29, 20

    Themagichereisthetwo@implementationand@interfacelines:@interfaceFraction(Math)and@implementationFraction(Math).

    Therecanonlybeonecategorywiththesamename.Additionalcateogiesmaybeaddedonwithdifferentbutunqiuenames.

    Categoriescan'taddinstancevariables.

  • 8/14/2019 Week 7 - Objective C - Tuesday

    22/23

    September 29, 20

    Categoriesallowustosplitupalargeclassintoseveralpieces.Eachcanbedevelopedandcreatedindependently,andthenbroughttogetherattheendtoformtheclass.

    Asaconsequencehowever,thisallowsussomeniceadvantages.Wecan,inessence,extendthefunctionalityofanyclassthatmayfallintoourhands,whetheritisinaclassyoucreateyourself,orinabinaryform,suchasintheCocoaframeworks,wecanaddacategorytoNSStringtoprovidefunctionalitythattheoriginalNSStringdidnotprovide.Categoriescanalsobeusedtooverridepreviousfunctionality,whichallowsusthento"patch"thesepreexistingclassesalso,whichmightbeprovidedwithabug.

  • 8/14/2019 Week 7 - Objective C - Tuesday

    23/23

    September 29, 20


Recommended