Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Objective-C vs Swift: 10 Differences

225 views

Published on

Being a software development providers ourselves, we noticed that some of Redwerk customers wonder about key differences between Objective-C and Swift. Understanding the importance of choosing the best technology to develop your project with, we created these easy-to-understand slides about before-mentioned technologies.

Also, you can find more information here: https://redwerk.com/blog/10-differences-objective-c-swift

Published in: Software
  • Be the first to comment

  • Be the first to like this

Objective-C vs Swift: 10 Differences

  1. 1. redwerk.com 10differencesbetween Swi &Objecve-C PRESENTATION
  2. 2. redwerk.com ThemaindifferencesbetweenSwi andObjecve-C 1.Op onals 2.ControlFlow 3.TypeInference 4.Tuples 5.StringManipulaon 6.GuardandDefer 7.7.FunconalProgrammingPa erns 8.Enumeraons 9.Funcons 10.DoStatement
  3. 3. redwerk.com 1.Op onals Op onalsallowfunconswhichmaynotalwaysbeabletoreturnameaningfulvaluetoreturn eitheravalueencapsulatedinanop onalornil.
  4. 4. redwerk.com 1.Op onals InCandObjecve-C,wecanreturnnilfromafunconthatwouldnormallyreturnanobject. Wecan’tdoitforfunconswhichareexpectedtoreturnabasictypesuchasint,float, ordouble.
  5. 5. redwerk.com 1.Op onals WithSwi,youdon’thaveworryaboutthis: Thecompileritselfwillraiseanerror,sincexisanop onalandcouldpoten allybenil.
  6. 6. redwerk.com 1.Op onals Beforeusinganop onalvalue,youneedtounwrapitlikeso: Alternately,wecanuseop onalbinding:
  7. 7. redwerk.com 2.ControlFlow InSwi,thecurlybraces({})arenotjustagoodidea,likeinCoraC-likelanguage,they’rethelaw! UnlikeObjecve-C,Swi treatscurlybracesasmandatoryforif,for,while,andrepeat statements.
  8. 8. redwerk.com 3.TypeInference InSwi,onceavariableisdeclaredwithaparculartype,itstypeisstacandcannotbechanged. Thecompilerisalsosmartenoughtofigureout(orinfer)whattypeyourvariablesshouldbebased onthevaluesyouassignthem.
  9. 9. redwerk.com 3.TypeInference Thecompilerwillraiseanerrorifyoutrytoassignanumericvalue,suchas10,toourstr2variable.
  10. 10. redwerk.com 3.TypeInference InObjecve-C,youmustalwaysstateavariable’stypeexplicitly.
  11. 11. redwerk.com 4.Tuples Swi supportstuples,valueswhichstoregroupsofothervalues,thatdon’thavetoallbethesame type.Forexample,youcanhaveatupleofStringsandInts:
  12. 12. redwerk.com 4.Tuples Themostobvioususefortuplesisreturningmulplevaluesfromafuncon.
  13. 13. redwerk.com 4.Tuples InObjecve-C,wecanuseblocksinsimilarfashion,butit’snotasstraigh orwardorelegant.
  14. 14. redwerk.com 5.StringManipula on Swi offershugeimprovementsoverObjecve-Cintermsofstringmanipulaon. Declareyourstringwithvarifyouwanttochangeitinthefuture,orwithletifyouneeditto remainconstant.
  15. 15. redwerk.com 5.StringManipula on InObjecve-C,concatenangtwoimmutablestringsrequiresyoutocreateanen relynew NSStringobject.
  16. 16. redwerk.com 5.StringManipula on Stringforma nginObjecve-Cinvolvesinserngspecialplaceholdersforeachdifferentdatatype wewanttointerpolateintoourstring.
  17. 17. redwerk.com 5.StringManipula on InSwi,wecaninsteadinterpolatevaluesbyinserngthevariablenamesdirectlyintoourstring.
  18. 18. redwerk.com 6.GuardandDefer Everheardofthe“pyramidof doom”?Ifnot,here’sasnippetof Objecve-Ccodethatshouldjog yourmemory:
  19. 19. redwerk.com 6.GuardandDefer Fortunately,inSwi,wehaveguard, anewcondionalstatementwhich canmakethiscodemuchmore readable.Guardstopsprogramflow ifacondionisnotmet:
  20. 20. redwerk.com 6.GuardandDefer Swi alsogivesusthedefer keyword,whichprovidesasafeand easywaytohandlecodewhichwe wantexecutedonlywhenthe programleavesthecurrentscope:
  21. 21. redwerk.com 6.GuardandDefer Asyoucansee,weneedtowrite fourseparatecallsto destData.dealloc(dataSize)tomake sureourpointergetsdeallocated evenifguardmakesthefunconexit intheeventofanerror.
  22. 22. redwerk.com 6.GuardandDefer Withdefer,wehaveahassle-free waytocleanupbothourpointerand ourcode:
  23. 23. redwerk.com 7.FunconalProgrammingPa erns Swi incorporatesanumberoffunconalprogrammingfeatures,suchasmapandfilter,which canbeusedonanycolleconwhichimplementstheColleconTypeprotocol:
  24. 24. redwerk.com 7.FunconalProgrammingPa erns Objecve-Chasnobuilt-insupportforfunconalprogramming. Tousethesesamekindsoffuncons,youwouldhavetouseathird-partylibrary.
  25. 25. redwerk.com 8.Enumera ons InSwi,enumeraonsaremorepowerfulthantheyeverwereinObjecve-C:theycannow containmethodsandbepassedbyvalue.IllustraonofenumsworkinSwi:
  26. 26. redwerk.com 8.Enumera ons Enumscanalsoberecursive,sowecanbuildalinkedlistusingtheindirectstatement,whichtells thecompilertoaddthenecessarylayerofindirecon:
  27. 27. redwerk.com 9.Funcons EveryfunconinSwi hasatype, whichconsistsofthefuncon’s parametertypesandreturntype.This meansyoucanassignfunconsto variablesorpassthemasparameters tootherfuncons:
  28. 28. redwerk.com 9.Funcons Swi alsoallowsyoutodefinedefaultvaluesforfunconparameters:
  29. 29. redwerk.com 10.Dostatement ThedostatementinSwi allowsyoutointroduceanewscope:
  30. 30. redwerk.com 10.Dostatement Dostatementscanalsocontainoneormorecatchclauses:
  31. 31. redwerk.com Conclusion Eventually,wecandisnguishsuchadvantagesofSwi: protocol-orientedprogramming; protocolscanbeimplementedbynotonlyclasses,butalsoenumsandstructsaswell; protocolsprovideanelegantandconfusion-freealternavetomulpleinheritance,asasingle objectcanimplementmulpleprotocols; light,flexiblesyntaxandnumerousfeaturestoguardagainstcommoncodingerrors.

×