WelcometoSwift
AboutSwift
Swiftisafantasticwaytowritesoftware,whetherit’sforphones,desktops,
servers,oranythingelsethatrunscode.It’sasafe,fast,andinteractive
programminglanguagethatcombinesthebestinmodernlanguagethinkingwith
wisdomfromthewiderAppleengineeringcultureandthediversecontributions
fromitsopen-sourcecommunity.Thecompilerisoptimizedforperformance
andthelanguageisoptimizedfordevelopment,withoutcompromisingoneither.
Swiftisfriendlytonewprogrammers.It’sanindustrial-qualityprogramming
languagethat’sasexpressiveandenjoyableasascriptinglanguage.Writing
Swiftcodeinaplaygroundletsyouexperimentwithcodeandseetheresults
immediately,withouttheoverheadofbuildingandrunninganapp.
Swiftdefinesawaylargeclassesofcommonprogrammingerrorsbyadopting
modernprogrammingpatterns:
Swiftcodeiscompiledandoptimizedtogetthemostoutofmodernhardware.
Thesyntaxandstandardlibraryhavebeendesignedbasedontheguiding
principlethattheobviouswaytowriteyourcodeshouldalsoperformthebest.
ItscombinationofsafetyandspeedmakeSwiftanexcellentchoicefor
everythingfrom“Hello,world!”toanentireoperatingsystem.
Swiftcombinespowerfultypeinferenceandpatternmatchingwithamodern,
lightweightsyntax,allowingcomplexideastobeexpressedinaclearand
concisemanner.Asaresult,codeisnotjusteasiertowrite,buteasiertoread
Variablesarealwaysinitializedbeforeuse.•
Arrayindicesarecheckedforout-of-boundserrors.•
Integersarecheckedforoverflow.•
Optionalsensurethatnilvaluesarehandledexplicitly.•
Memoryismanagedautomatically.•
Errorhandlingallowscontrolledrecoveryfromunexpectedfailures.•
andmaintainaswell.
Swifthasbeenyearsinthemaking,anditcontinuestoevolvewithnewfeatures
andcapabilities.OurgoalsforSwiftareambitious.Wecan’twaittoseewhat
youcreatewithit.
VersionCompatibility
ThisbookdescribesSwift4.1,thedefaultversionofSwiftthat’sincludedin
Xcode9.2.YoucanuseXcode9.2tobuildtargetsthatarewrittenineitherSwift
4orSwift3.
N O T E
WhentheSwift4compilerisworkingwithSwift3code,itidentifiesitslanguage
versionas3.2.Asaresult,youcanuseconditionalcompilationblockslike#if
swift(>=3.2)towritecodethat’scompatiblewithmultipleversionsoftheSwift
compiler.
WhenyouuseXcode9.2tobuildSwift3code,mostofthenewSwift4
functionalityisavailable.Thatsaid,thefollowingfeaturesareavailableonlyto
Swift4code:
AtargetwritteninSwift4candependonatargetthat’swritteninSwift3,and
viceversa.Thismeans,ifyouhavealargeprojectthat’sdividedintomultiple
frameworks,youcanmigrateyourcodefromSwift3toSwift4oneframework
atatime.
SubstringoperationsreturnaninstanceoftheSubstringtype,instead
ofString.
•
The@objcattributeisimplicitlyaddedinfewerplaces.•
Extensionstoatypeinthesamefilecanaccessthattype’sprivate
members.
•