swift demo copy - computer science & e · 2015-11-24 · • swift is primarily used to create...
Post on 18-Jul-2020
4 Views
Preview:
TRANSCRIPT
Swift
Jeremy DaySteven DaoNicole Lazar
Overview
• SwiftisprimarilyusedtocreateapplicationsforApple’siOSplatform,aswellastheOSXoperatingsystem.
• Swiftisamulti-paradigmlanguage.Itfeatures:theprotocol-orientedprogrammingparadigm,object-orientedprogramming,functionalprogramming,imperativeprogramming,andblockstructuring.
• SwiftwasintroducedasanalternativetousingObjective-CforiOSdevelopment.AccordingtotheJune2015RedMonk ProgrammingLanguagerankings,Objective-Cisstillmorepopularthanswift(#10vs#18),butSwift’srankingisstillimpressiveduetothefactitwasonlyannouncedinJuneoflastyear.
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.”
ProblemDomain
• Swift’sprimaryuseisinbuildingapplicationsforiOS,whichistheoperatingsystemonmostApple’smobiledevicesincludingtheiPhoneandiPad.ProgramswritteninSwiftwillalsoworkonwatchOSandtvOS,theoperatingsystemsforAppleWatchandAppleTv,respectively.
• Swiftstartedoutasaproprietarylanguage,butwilltransitionintoopensourcelaterthisyear.Afteritstransitiontoopensource,SwiftapplicationswillbeabletorunonOSXandLinuxaswell.
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
LanguageDesign&Concepts
Optionals:
• Thisnewfeature inSwiftallowsreferences orvaluestooperateinamannersimilartothecommonpatterninC,whereapointermayrefer toavalueormaybenull.
• Thisimpliesthatnon-optionaltypescannotresultinanull-pointererror;thecompilercanensurethisisnotpossible.
• Toindicatethatavariableisoptional,thequestionmarkoperatorisplacedafterthevariablename.
• Theexclamationpointoperatorisusedtoforceunwraptheoptional,onlyifitisknowntonotbeemptyornull.
var age: Int? = nilvar height: Int? = 180
Protocols:
• AlsoafeatureofObjective-C,protocolsareusedtoensurethataclassimplementsasetofmethods,justlikeinterfacesdoinotherlanguages.
Tuples:
• BecauseSwiftcontainsadvancedtypes,suchastuples,itallowsforfunctionstoreturnmorethanonevalue.
• Swiftalsosupportspatternmatchinginswitchstatements.
LanguageDesign&Concepts
Title
Message
Ok Button
Drag 2 Text Fields to Storyboard
Drag 1 Button to Storyboard
Connection from Storyboard to Code:Ctrl + Drag Text Field to Swift File
Connection from Storyboard to Code:Ctrl + Drag Button to Swift File
Select Action for Connection Type
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.”
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/
Swift
Jeremy DaySteven DaoNicole Lazar
top related