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

Contenu connexe

Similaire à Objective-C vs Swift: 10 Differences

Sony C#/.NET component set analysis
Sony C#/.NET component set analysisSony C#/.NET component set analysis
Sony C#/.NET component set analysisPVS-Studio
 
Re-checking the ReactOS project - a large report
Re-checking the ReactOS project - a large reportRe-checking the ReactOS project - a large report
Re-checking the ReactOS project - a large reportPVS-Studio
 
Archeology for Entertainment, or Checking Microsoft Word 1.1a with PVS-Studio
Archeology for Entertainment, or Checking Microsoft Word 1.1a with PVS-StudioArcheology for Entertainment, or Checking Microsoft Word 1.1a with PVS-Studio
Archeology for Entertainment, or Checking Microsoft Word 1.1a with PVS-StudioAndrey Karpov
 
Dependency Injection and Autofac
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofacmeghantaylor
 
I just had to check ICQ project
I just had to check ICQ projectI just had to check ICQ project
I just had to check ICQ projectPVS-Studio
 
PVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's codePVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's codeAndrey Karpov
 
Re usable continuous-time analog sva assertions
Re usable continuous-time analog sva assertionsRe usable continuous-time analog sva assertions
Re usable continuous-time analog sva assertionsRégis SANTONJA
 
啄木鸟Twisted
啄木鸟Twisted啄木鸟Twisted
啄木鸟TwistedXuYj
 
DevOps for the Enterprise: Automating Deployments
DevOps for the Enterprise: Automating DeploymentsDevOps for the Enterprise: Automating Deployments
DevOps for the Enterprise: Automating DeploymentsAmazon Web Services
 
The First C# Project Analyzed
The First C# Project AnalyzedThe First C# Project Analyzed
The First C# Project AnalyzedPVS-Studio
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJSAnass90
 
Source code of WPF samples by Microsoft was checked
Source code of WPF samples by Microsoft was checkedSource code of WPF samples by Microsoft was checked
Source code of WPF samples by Microsoft was checkedPVS-Studio
 
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017Andrey Karpov
 
Real World Single Page App - A Knockout Case Study
Real World Single Page App - A Knockout Case StudyReal World Single Page App - A Knockout Case Study
Real World Single Page App - A Knockout Case Studyhousecor
 
Python and Ruby implementations compared by the error density
Python and Ruby implementations compared by the error densityPython and Ruby implementations compared by the error density
Python and Ruby implementations compared by the error densityPVS-Studio
 

Similaire à Objective-C vs Swift: 10 Differences (20)

Thinking In Swift
Thinking In SwiftThinking In Swift
Thinking In Swift
 
Sony C#/.NET component set analysis
Sony C#/.NET component set analysisSony C#/.NET component set analysis
Sony C#/.NET component set analysis
 
Robots in Swift
Robots in SwiftRobots in Swift
Robots in Swift
 
Re-checking the ReactOS project - a large report
Re-checking the ReactOS project - a large reportRe-checking the ReactOS project - a large report
Re-checking the ReactOS project - a large report
 
Archeology for Entertainment, or Checking Microsoft Word 1.1a with PVS-Studio
Archeology for Entertainment, or Checking Microsoft Word 1.1a with PVS-StudioArcheology for Entertainment, or Checking Microsoft Word 1.1a with PVS-Studio
Archeology for Entertainment, or Checking Microsoft Word 1.1a with PVS-Studio
 
Dependency Injection and Autofac
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofac
 
I just had to check ICQ project
I just had to check ICQ projectI just had to check ICQ project
I just had to check ICQ project
 
PVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's codePVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's code
 
C prog
C progC prog
C prog
 
Re usable continuous-time analog sva assertions
Re usable continuous-time analog sva assertionsRe usable continuous-time analog sva assertions
Re usable continuous-time analog sva assertions
 
Start with swift
Start with swiftStart with swift
Start with swift
 
啄木鸟Twisted
啄木鸟Twisted啄木鸟Twisted
啄木鸟Twisted
 
DevOps for the Enterprise: Automating Deployments
DevOps for the Enterprise: Automating DeploymentsDevOps for the Enterprise: Automating Deployments
DevOps for the Enterprise: Automating Deployments
 
The First C# Project Analyzed
The First C# Project AnalyzedThe First C# Project Analyzed
The First C# Project Analyzed
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
Source code of WPF samples by Microsoft was checked
Source code of WPF samples by Microsoft was checkedSource code of WPF samples by Microsoft was checked
Source code of WPF samples by Microsoft was checked
 
Neighborly nagios
Neighborly nagiosNeighborly nagios
Neighborly nagios
 
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
 
Real World Single Page App - A Knockout Case Study
Real World Single Page App - A Knockout Case StudyReal World Single Page App - A Knockout Case Study
Real World Single Page App - A Knockout Case Study
 
Python and Ruby implementations compared by the error density
Python and Ruby implementations compared by the error densityPython and Ruby implementations compared by the error density
Python and Ruby implementations compared by the error density
 

Dernier

What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfYashikaSharma391629
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxRTS corp
 

Dernier (20)

What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
 

Objective-C vs Swift: 10 Differences