Mbile/Wireless Systems Swift Yanyan Zhuang Department f Cmputer Science http://www.cs.uccs.edu/~yzhuang UC. Clrad Springs
cat annunce.txt_ imacs remte VNC access VNP: http://www.uccs.edu/itservices/services/netwrk-andinternet/vpn.html VNC passwrd: cs5530 Please save data t Z Please d nt use imacs in Library IT will upgrade 2
Swift What is it? A new prgramming language fr Apple prducts } ios (ipds, iphnes, ipads, etc.), macos, watchos, tvos, future } Currently at versin 3 T see yur versin: xcrun swift -versin Apple Swift versin 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1) } Open surce Based n Objective-C and C. } Classes, instances, prperties, methds, inheritance, etc. 3
Swift Requires an Apple prduct fr develpment } Air, MacBk, MacBk Pr, imac, itrashcan (MacPr) Requires the Xcde develpment envirnment, Apple nly. Resurces at: } https://develper.apple.cm/ 4
Xcde Playgrund An interactive wrk envirnment that allws yu update values real-time and see results. A prject ptin in Xcde. New fr ipad ios 10!!! 5
Xcde Playgrund 6
Swift Quick verview f the language Assignments Cntrl Flw Functins and Clsures Objects and Classes Enumeratins and Structures Prtcls Errr Handling 7
Swift - Overview Dn t need t imprt a separate library fr functinality like input/utput r string handling. Cde written at glbal scpe is used as the entry pint fr the prgram, s yu dn t need main(). Dn t need t write semiclns at the end f every statement. Excerpt Frm: Apple Inc. The Swift Prgramming Language (Swift 3.0.1). ibks. https://itun.es/ca/jeuh0.l 8
Swift - Assignments Key wrd Descriptin let var Types can be inferred Can be explicit NO implicit type cnversins } Values in strings by using a \ let apples = 3 Used fr cnstants. Des nt need t be knwn at cmpile time but must be assigned a value exactly nce. Used fr variables. let applysummary = I have \(apples) apples. 9
Swift - Assignments Key wrd let var Descriptin Used fr cnstants. Des nt need t be knwn at cmpile time but must be assigned a value exactly nce. Used fr variables. Types can be inferred Can be explicit NO implicit type cnversins } Values in strings by using a \ let apples = 3 let applysummary = I have \(apples) apples. } Values are never implicitly cnverted t anther type. If need t cnvert a value t a different type, explicitly make an instance f the desired type. The Swift Prgramming Language (Swift 3.0.1). 10
Swift - Assignments Assignments Dictinaries and arrays use [] var shppinglist = [ hp, apple, micrsft ] shppinglist[1] = Lenv var ccupatins = ["Malclm": "Captain", "Kaylee": "Mechanic"] ccupatins["jayne"] = "Public Relatins Empty arrays r dictinaries let emptyarray = [String]() Let emptydictinary = [String: Flat]() If type infrmatin can be inferred, can write an empty array as [] and an empty dictinary as [:] Data Types Typical data types available. } String, Flat, Duble, Bl, Int/Uint, Character, Optinal 11
Swift Cntrl Flw Keywrd if, switch fr-in, fr, while, repeat-while Fr/if example Descriptin If cnditin must be explicit if scre {..} is an errr Used fr cnditinals. Parenthesis arund variable are ptinal. Braces arund cnditinal bdy are required. Used fr lps. Parenthesis arund variable are ptinal. Braces arund lp bdy are required. 12
Swift Cntrl Flw Switch 13
Swift Cntrl Flw Switch let can be used in a pattern t assign value N need t break } Only ne match 14
Swift Cntrl Flw fr-in Iterate ver items in a dictinary by prviding a pair f names t use fr each key-value pair. Dictinaries are unrdered! 15
Swift Cntrl Flw While & repeat-while } Same as C r Java s while & d-while. } repeat { } while sme-cnditin Fr lps still the same } Thugh yu can use..< r... t make ranges. 0..<7 nn-inclusive upper bund. fr i in 0..<7 { } 0...7 inclusive upper bund fr i in 0...7 { } 16
Swift Functins & Clsures Use func t declare a functin à t indicate return type Use a tuple t make a cmpund value: return multiple values frm a functin } Elements f a tuple can be referred t by name r by number } Defined as à (min: Int, max: Int, sum: Int) } Access as results.sum, r results.2 17
Swift Functins & Clsures Can take variable arguments, cllects int an array fr yu. Can be nested. 18
Swift Functins & Clsures Functins are first-class types: they can return anther functin as a return-value Can take anther functin as ne f its arguments 19
Swift Functins & Clsures A clsure is a blck f cde that can be called later (annymus functin) Cde in a clsure has access t Variables and functins that were available in the scpe where the clsure was created, even if the clsure is in a different scpe when it is executed Yu can write a clsure withut a name (functin name) } Surrund cde with braces {} } Use in t separate the arguments and return type frm the bdy Syntax: Indicates that definitin f clsure s parameters and return type has finished, and the bdy f the clsure is abut t begin { (parameters) -> return type in } statements 20
Swift Functins & Clsures Cncise 1: if type already knwn, yu can mit types f parameters and/r return type. Cncise 2: can refer t parameters by number instead f name 21
Swift Objects & Classes Classes As we d expect. Use init as initializer / cnstructr. Use deinit as deinitializer / destructr Instantiatin by referencing class name fllwed by () } var shape = Shape() 22
Swift Objects & Classes Classes T inherit, subclasses include their super classes name after their class name, separated by a : } class Square: Shape } class ViewCntrller: UIViewCntrller, UITextFieldDelegate Methds in a subclass that verride the superclass s implementatin are marked with verride } Overriding a methd by accident, withut verride, is detected by the cmpiler as an errr 23
Swift Objects & Classes Prperties can have getter and setter methds. } Similar t Java, C#, VB.Net } Nte newvalue is implicitly defined fr us as the new value (see cde example) } Can be explicit by declaring the setter as: set(<parameter_name>) set( myside ) {... } There is n type declaratin needed because the prperty defined it. 24
Swift Objects & Classes Inheritance } Class: parent } Over ride with verride keywrd. } Call parent methds with super. keywrd. 25
Swift Enumeratins & Structures Enumeratins Use enum t create an enumeratin } Swift assigns raw values starting at zer and increments by 1, but can change this by explicitly specifying values Can have methds assciated with them. 26
Swift Enumeratins & Structures Structures Use struct t create a structure. Supprt many f the same behavirs as classes, including methds & initializers. Structures are passed by value! (classes by reference) 27
Swift Prtcls & Extensins Prtcls It s basically an interface frm ther OO languages. Use prtcl t declare a prtcl. mutating indicates a functin changing the struct. } Nt needed in class redefinitins as class methds can always mdify the class. } Needed in structures t indicate that the methd will mdify the structure. Classes, enumeratins and structs can all adpt prtcls. 28
Swift Prtcls & Extensins Use extensins t add functinality t an existing type 29
Swift Errr Handling Errr Handling Represent errrs using any type that adpts the Errr prtcl. Use thrw t thrw an errr and thrws t dente a functin that can thrw an errr. 30
Swift Errr Handling Errr Handling d / catch } In d blck, mark cde that can thrw an errr by writing try in frnt } In catch blck, the errr is autmatically given the name errr unless yu give it a different name } Can prvide multiple catch blcks that handle specific errrs 31
Swift Cmments 32
Let s Practice! Print strings (use terminatr: t disable \n) let label = "The width is " let width = 94 print(label+string(width)) // cmpare with print(label+string(width), terminatr: ) let apples = 3 let applesummary = "I have \(apples) apples." let ranges = 5 let fruitsummary = "I have \(apples+ranges) pieces f fruit." 33
Let s Practice! Q1: What s wrng with the fllwing cde? Q2: 34
Swift Resurces Cntent was used frm these web sites where apprpriate. These sites cntain quite a bit mre infrmatin and wuld make a great resurce fr yu. https://develper.apple.cm/ https://www.hackingwithswift.cm/read https://www.hackingwithswift.cm/example-cde https://itunes.apple.cm/us/bk/the-swift-prgramminglanguage/id881256329?mt=11 35