swift demo copy - computer science & e · 2015-11-24 · • swift is primarily used to create...

16
Swift Jeremy Day Steven Dao Nicole Lazar

Upload: others

Post on 18-Jul-2020

4 views

Category:

Documents


0 download

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

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

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

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

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

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

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

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

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

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

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

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

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
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

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

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

Swift

Jeremy DaySteven DaoNicole Lazar