swift demo copy - computer science & e · 2015-11-24 · • swift is primarily used to create...
TRANSCRIPT
![Page 1: Swift Demo copy - Computer Science & E · 2015-11-24 · • Swift is primarily used to create applications for Apple’s iOS platform, as well as the OS X operating system. • Swift](https://reader033.vdocument.in/reader033/viewer/2022042709/5f5562c2d325ff71e2470304/html5/thumbnails/1.jpg)
Swift
Jeremy DaySteven DaoNicole Lazar
![Page 2: Swift Demo copy - Computer Science & E · 2015-11-24 · • Swift is primarily used to create applications for Apple’s iOS platform, as well as the OS X operating system. • Swift](https://reader033.vdocument.in/reader033/viewer/2022042709/5f5562c2d325ff71e2470304/html5/thumbnails/2.jpg)
Overview
• SwiftisprimarilyusedtocreateapplicationsforApple’siOSplatform,aswellastheOSXoperatingsystem.
• Swiftisamulti-paradigmlanguage.Itfeatures:theprotocol-orientedprogrammingparadigm,object-orientedprogramming,functionalprogramming,imperativeprogramming,andblockstructuring.
• SwiftwasintroducedasanalternativetousingObjective-CforiOSdevelopment.AccordingtotheJune2015RedMonk ProgrammingLanguagerankings,Objective-Cisstillmorepopularthanswift(#10vs#18),butSwift’srankingisstillimpressiveduetothefactitwasonlyannouncedinJuneoflastyear.
![Page 3: Swift Demo copy - Computer Science & E · 2015-11-24 · • Swift is primarily used to create applications for Apple’s iOS platform, as well as the OS X operating system. • Swift](https://reader033.vdocument.in/reader033/viewer/2022042709/5f5562c2d325ff71e2470304/html5/thumbnails/3.jpg)
HistoryofSwift
• SwiftwasfirstannouncedbyAppleatWWDConJune2,2014.
• Applealsoreleased theirmanualonSwift,TheSwiftProgrammingLanguageatthesametime.
• SwiftleftbetaonSeptember9th,2014.Severalnewiterationsoftheversionhavebeenreleasedsincethen,withSwift2.0beingannouncedatWWDCthisyear.
• DevelopmentofSwiftwasstartedin2010byChrisLattner,butmanyengineersatApplehadapartinSwift’sdevelopment.
• AccordingtoChrisLattner,Swiftdrawsideasfrom“Objective-C,Rust,Haskell,Ruby,Python,C#,CLU,andfartoomanyotherstolist.”
![Page 4: Swift Demo copy - Computer Science & E · 2015-11-24 · • Swift is primarily used to create applications for Apple’s iOS platform, as well as the OS X operating system. • Swift](https://reader033.vdocument.in/reader033/viewer/2022042709/5f5562c2d325ff71e2470304/html5/thumbnails/4.jpg)
ProblemDomain
• Swift’sprimaryuseisinbuildingapplicationsforiOS,whichistheoperatingsystemonmostApple’smobiledevicesincludingtheiPhoneandiPad.ProgramswritteninSwiftwillalsoworkonwatchOSandtvOS,theoperatingsystemsforAppleWatchandAppleTv,respectively.
• Swiftstartedoutasaproprietarylanguage,butwilltransitionintoopensourcelaterthisyear.Afteritstransitiontoopensource,SwiftapplicationswillbeabletorunonOSXandLinuxaswell.
![Page 5: Swift Demo copy - Computer Science & E · 2015-11-24 · • Swift is primarily used to create applications for Apple’s iOS platform, as well as the OS X operating system. • Swift](https://reader033.vdocument.in/reader033/viewer/2022042709/5f5562c2d325ff71e2470304/html5/thumbnails/5.jpg)
LanguageDesign&Concepts
Java Swift
String x = “Hello”; var x = “Hello”var x: String = “Hello”
Objective-C Interoperability:
• BecauseSwiftstillhasaccesstoObjective-ClibrariesandAPIs,thetwolanguagescanco-existtogether.
Syntacticsugar:
• Usethevar keywordtodefineavariable
• Usethelet keywordtodefineaconstant
TypeInference:
• Infers thetypebasedonthevalue
![Page 6: Swift Demo copy - Computer Science & E · 2015-11-24 · • Swift is primarily used to create applications for Apple’s iOS platform, as well as the OS X operating system. • Swift](https://reader033.vdocument.in/reader033/viewer/2022042709/5f5562c2d325ff71e2470304/html5/thumbnails/6.jpg)
LanguageDesign&Concepts
Optionals:
• Thisnewfeature inSwiftallowsreferences orvaluestooperateinamannersimilartothecommonpatterninC,whereapointermayrefer toavalueormaybenull.
• Thisimpliesthatnon-optionaltypescannotresultinanull-pointererror;thecompilercanensurethisisnotpossible.
• Toindicatethatavariableisoptional,thequestionmarkoperatorisplacedafterthevariablename.
• Theexclamationpointoperatorisusedtoforceunwraptheoptional,onlyifitisknowntonotbeemptyornull.
var age: Int? = nilvar height: Int? = 180
![Page 7: Swift Demo copy - Computer Science & E · 2015-11-24 · • Swift is primarily used to create applications for Apple’s iOS platform, as well as the OS X operating system. • Swift](https://reader033.vdocument.in/reader033/viewer/2022042709/5f5562c2d325ff71e2470304/html5/thumbnails/7.jpg)
Protocols:
• AlsoafeatureofObjective-C,protocolsareusedtoensurethataclassimplementsasetofmethods,justlikeinterfacesdoinotherlanguages.
Tuples:
• BecauseSwiftcontainsadvancedtypes,suchastuples,itallowsforfunctionstoreturnmorethanonevalue.
• Swiftalsosupportspatternmatchinginswitchstatements.
LanguageDesign&Concepts
![Page 8: Swift Demo copy - Computer Science & E · 2015-11-24 · • Swift is primarily used to create applications for Apple’s iOS platform, as well as the OS X operating system. • Swift](https://reader033.vdocument.in/reader033/viewer/2022042709/5f5562c2d325ff71e2470304/html5/thumbnails/8.jpg)
Title
Message
Ok Button
![Page 9: Swift Demo copy - Computer Science & E · 2015-11-24 · • Swift is primarily used to create applications for Apple’s iOS platform, as well as the OS X operating system. • Swift](https://reader033.vdocument.in/reader033/viewer/2022042709/5f5562c2d325ff71e2470304/html5/thumbnails/9.jpg)
Drag 2 Text Fields to Storyboard
![Page 10: Swift Demo copy - Computer Science & E · 2015-11-24 · • Swift is primarily used to create applications for Apple’s iOS platform, as well as the OS X operating system. • Swift](https://reader033.vdocument.in/reader033/viewer/2022042709/5f5562c2d325ff71e2470304/html5/thumbnails/10.jpg)
Drag 1 Button to Storyboard
![Page 11: Swift Demo copy - Computer Science & E · 2015-11-24 · • Swift is primarily used to create applications for Apple’s iOS platform, as well as the OS X operating system. • Swift](https://reader033.vdocument.in/reader033/viewer/2022042709/5f5562c2d325ff71e2470304/html5/thumbnails/11.jpg)
Connection from Storyboard to Code:Ctrl + Drag Text Field to Swift File
![Page 12: Swift Demo copy - Computer Science & E · 2015-11-24 · • Swift is primarily used to create applications for Apple’s iOS platform, as well as the OS X operating system. • Swift](https://reader033.vdocument.in/reader033/viewer/2022042709/5f5562c2d325ff71e2470304/html5/thumbnails/12.jpg)
Connection from Storyboard to Code:Ctrl + Drag Button to Swift File
Select Action for Connection Type
![Page 13: Swift Demo copy - Computer Science & E · 2015-11-24 · • Swift is primarily used to create applications for Apple’s iOS platform, as well as the OS X operating system. • Swift](https://reader033.vdocument.in/reader033/viewer/2022042709/5f5562c2d325ff71e2470304/html5/thumbnails/13.jpg)
![Page 14: Swift Demo copy - Computer Science & E · 2015-11-24 · • Swift is primarily used to create applications for Apple’s iOS platform, as well as the OS X operating system. • Swift](https://reader033.vdocument.in/reader033/viewer/2022042709/5f5562c2d325ff71e2470304/html5/thumbnails/14.jpg)
Comparison(Objective-C)•Swiftiseasiertoread
•Nosemicolonsorparentheses forif-elsestatements
•Swiftrequireslesscode
•Swiftisfasterandsafer
•Optionaltyping
var x = 1var y = "Hello”
var x: Intx = 2
Objective-C Swift
NSString *person= @”John";int age= 21;NSString *s = [NSString stringWithFormat:@”%@ is %d years old", person, age];
let person = ”John"let age = 21let s = “\(person) is \(age) years old.”
![Page 15: Swift Demo copy - Computer Science & E · 2015-11-24 · • Swift is primarily used to create applications for Apple’s iOS platform, as well as the OS X operating system. • Swift](https://reader033.vdocument.in/reader033/viewer/2022042709/5f5562c2d325ff71e2470304/html5/thumbnails/15.jpg)
References• http://www.infoworld.com/article/2920333/mobile-development/swift-vs-objective-c-10-reasons-the-future-favors-swift.html
• http://www.toptal.com/swift/from-objective-c-to-swift
• http://nondot.org/sabre/
• http://www.apple.com/live/2015-june-event/
• https://developer.apple.com/swift/
• https://redmonk.com/sogrady/2015/07/01/language-rankings-6-15/
• http://www.touch-code-magazine.com/swift-optionals-use-let/
![Page 16: Swift Demo copy - Computer Science & E · 2015-11-24 · • Swift is primarily used to create applications for Apple’s iOS platform, as well as the OS X operating system. • Swift](https://reader033.vdocument.in/reader033/viewer/2022042709/5f5562c2d325ff71e2470304/html5/thumbnails/16.jpg)
Swift
Jeremy DaySteven DaoNicole Lazar