Swift. Mobile Application Development in ios School of EECS Washington State University Instructor: Larry Holder

Similar documents
Welcome to Swift

Gerontechnology II. Collecting Smart Phone Sensor Data for Gerontechnology. Using ios

COMP327 Mobile Computing Session:

Settings. Mobile Application Development in ios School of EECS Washington State University Instructor: Larry Holder

SWIFT! init(title: String) { self.title = title } // required initializer w/ named parameter

CSC 581: Mobile App Development Spring 2019

Miscellaneous Topics

ios Application Development Lecture 4: Navigation and Workflow

lecture 1 hello, swift cs : spring 2018

Introduction to Swift. Dr. Sarah Abraham

ITP 342 Mobile App Dev. Functions

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

Polymorphism COSC346

SWIFT BASICS

lecture 6 Closures, Networking, CocoaPods

Swift. Introducing swift. Thomas Woodfin

SWIFT - PROTOCOLS. Protocols also follow the similar syntax as that of classes, structures, and enumerations:

iphone Application Programming Lecture 3: Swift Part 2

iphone Application Programming Lecture 3: Swift Part 2

Introduce C# as Object Oriented programming language. Explain, tokens,

UI Design and Storyboarding

Developing applications for ios

Building a testable mixed-codebase ios Framework

COMP-520 GoLite Tutorial

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Introduction to C# Applications

COMP520 - GoLite Type Checking Specification

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

Don t Go Java! Edition 2019

I/O in Haskell. To output a character: putchar :: Char -> IO () e.g., putchar c. To output a string: putstr :: String -> IO () e.g.

The Pyth Language. Administrivia

Try the following example using Try it option available at the top right corner of the following sample code box:

Basic Types, Variables, Literals, Constants

Xcode & Swift: Introduction

COMP520 - GoLite Type Checking Specification

Procedures, Parameters, Values and Variables. Steven R. Bagley

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Swift by Practical Example. Justin Miller

Questions. Exams: no. Get by without own Mac? Why ios? ios vs Android restrictions. Selling in App store how hard to publish? Future of Objective-C?

CMSC 330: Organization of Programming Languages

CS2141 Software Development using C/C++ C++ Basics

Ruby: Introduction, Basics

Optimizing Swift Performance Session 409

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Enhancing your apps for the next dimension of touch

CSE 431S Type Checking. Washington University Spring 2013

Mobile Development - Lab 2

Introduction CMSC 330: Organization of Programming Languages. Books on Ruby. Applications of Scripting Languages

ios Application Development Lecture 3: Unit 2

CMSC 330: Organization of Programming Languages

Richard Mallion. Swift for Admins #TEAMSWIFT

What s New in Swift #WWDC18. Ted Kremenek, Languages & Runtimes Manager Slava Pestov, Swift Compiler Engineer

Type Conversion. and. Statements

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Media Playback and Recording. CS193W - Spring Lecture 3

lecture 11 Advanced Swift

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

C#.Net. Course Contents. Course contents VT BizTalk. No exam, but laborations

CS558 Programming Languages

ios Application Development Lecture 2: Seminar and Unit 1

Building Faster in Xcode

Introduction to OCaml

Duration 5 days (For basic crowd 5+3days needed)

3. Java - Language Constructs I

Swift-Tcl. Bridging between Swift and Tcl. Why Swift? - Swift vs C and Swift vs Tcl. Peter da Silva. Tcl October 24, 2016

PRO SWIFT BOOK AND VIDEOS M A S E. Break out of beginner s Swift. with this hands-on guide

CMSC 330: Organization of Programming Languages. OCaml Data Types

Mobile Application Programming. Swift Classes

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Tables. Mobile Application Development in ios School of EECS Washington State University Instructor: Larry Holder

CS558 Programming Languages

CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

A Type is Worth a Thousand Tests

Data Storage. Mobile Application Development in ios School of EECS Washington State University Instructor: Larry Holder

The role of semantic analysis in a compiler

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Stanford CS193p. Developing Applications for ios. Winter CS193p. Winter 2017

OBJECT ORIENTED PROGRAMMING

Final thoughts on functions F E B 2 5 T H

iphone Application Programming Lab 3: Swift Types and Custom Operator + A02 discussion

SWIFT & #IOExtendedCLT, 18th May 2016

(infinitespeak.wordpress.com) Classes and Structs. Dr. Sarah Abraham

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

A Fast Review of C Essentials Part I

Stanford CS193p. Developing Applications for ios. Winter CS193p. Winter 2017

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

learning objectives learn about variables, their types and their values learn about different number representations

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

The current topic: Python. Announcements. Python. Python

The Swift Programming Language

COMP520 - GoLite Type Checking Specification

Notifications. Mobile Application Development in ios School of EECS Washington State University Instructor: Larry Holder

Introduction to Functional Programming in Haskell 1 / 56

What s New in LLDB. Debug your way to fame and glory #WWDC15. Developer Tools. Session 402

Understanding Undefined Behavior

C#: framework overview and in-the-small features

6.096 Introduction to C++ January (IAP) 2009

Final CSE 131 Winter 2010

Reverse Engineering Swift Apps. Michael Gianarakis Rootcon X 2016

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Transcription:

Swift Mobile Application Development in ios School of EECS Washington State University Instructor: Larry Holder Mobile Application Development in ios 1

Why Swift Recommended for all ios, macos, watchos, and tvos app development Designed by Apple, but now open source Available for Windows and Linux too Faster than Python, and C++ in some cases Can directly call C/C++ code Swift vs. Python, C++, Java, Go, Mobile Application Development in ios 2

Xcode Playgrounds Blank Game Map Single View Mobile Application Development in ios 3

Good Tutorials The Swift Programming Language (Swift 4) developer.apple.com/library/content/documentation/ Swift/Conceptual/Swift_Programming_Language The Swift Tour Includes Playground for all code Ray Wenderlich http://raywenderlich.com/category/swift Mobile Application Development in ios 4

Caveat Assume proficiency in some object-oriented language (e.g., C++, Java, Python) Mobile Application Development in ios 5

Constants, Variables & Types Constants (let) vs. variables (var) Types (convention: first letter capitalized) Basic types: Bool, Int, Float, Double, String Collection types: Array, Set, Dictionary Tuples var shoppinglist = ["coffee": 3, "candy": 4] for (item, amount) in shoppinglist { print("\(item): \(amount)") Mobile Application Development in ios 6

Optionals Optional variable can be nil or hold a value let possiblestr: String? = "Hello" // optional type print(possiblestr) // outputs "Optional("Hello"), warning let forcedstr: String = possiblestr! // unwrapping print(forcedstr) // outputs "Hello" let assumedstr: String! = "Hello" // implicitly unwrapped let implicitstr: String = assumedstr // no need for! Optional binding if let tempstr = possiblestr { print(tempstr) Mobile Application Development in ios 7

Range Operators Range operators (a b, a..<b) let count = 5 for index in 1...count { print("\(index)") // 1 2 3 4 5 for index in 0..<count { print("\(index)") // 0 1 2 3 4 Mobile Application Development in ios 8

Functions func fahrenheittocelsius (temp: Float) -> Float { let tempc = (temp - 32.0) * 5.0 / 9.0 return tempc func printcelsius (temp tempf: Float) { let tempc = fahrenheittocelsius(temp: tempf) print("\(tempf) F = \(tempc) C") func printf2ctable (_ low: Int = 0, _ high: Int = 100) { for temp in low...high { printcelsius(temp: Float(temp)) printf2ctable() printf2ctable(70) printf2ctable(70,80) Mobile Application Development in ios 9

Function Types func addtwoints (_ a: Int, _ b: Int) -> Int { return a + b var mathfunction: (Int, Int) -> Int = addtwoints print("result: \(mathfunction(2, 3))") // prints Result: 5 func printmathresult (_ mathfunction: (Int, Int) -> Int, _ a: Int, _ b: Int) { print("result: \(mathfunction(a, b))") printmathresult(addtwoints, 3, 5) // prints Result: 8 Mobile Application Development in ios 10

Closures Self-contained block of code Can capture references to variables in context General form: { (parameters) -> return-type in statements Mobile Application Development in ios 11

Closures (cont.) let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"] func backward(_ s1: String, _ s2: String) -> Bool { return s1 > s2 var reversednames = names.sorted (by: backward) reversednames = names.sorted (by: { (s1: String, s2: String) -> Bool in return s1 > s2 ) Mobile Application Development in ios 12

Closures: Capturing Values func makeincrementer(forincrement amount: Int) -> () -> Int { var runningtotal = 0 func incrementer() -> Int { runningtotal += amount return runningtotal return incrementer let incrementbyten = makeincrementer(forincrement: 10) incrementbyten() // returns a value of 10 incrementbyten() // returns a value of 20 incrementbyten() // returns a value of 30 Mobile Application Development in ios 13

Escaping Closures Closure passed to function, but called after function returns var completionhandlers: [() -> Void] = [] func addcompletionhandler (handler: @escaping () -> Void) { completionhandlers.append(handler) func printhello() { print("hello") addcompletionhandler(handler: printhello) for handler in completionhandlers { handler() Mobile Application Development in ios 14

Enumerations enum Direction { case up // does not imply.up = 0 case left case down case right var playerdirection = Direction.right playerdirection =.up // type inference func turnleft (direction: Direction) -> Direction { var newdirection: Direction switch direction { case.up: newdirection =.left case.left: newdirection =.down case.down: newdirection =.right case.right: newdirection =.up return newdirection // no break Mobile Application Development in ios 15

Raw values Enumerations (cont.) func facingleftorright (direction: Direction) -> Bool { switch direction { case.left,.right: return true default: return false enum Direction: Int { case up = 0, left, down, right // now they re Int s Direction.left.rawValue // equals 1 enum Direction: String { case up, left, down, right // now they re String s Direction.left.rawValue // equals left Mobile Application Development in ios 16

Classes class Player { var direction: Direction var speed: Float var inventory: [String]? // initialized to nil // init required to set uninitialized variables init (speed: Float, direction: Direction) { self.speed = speed self.direction = direction func energize() { speed += 1.0 var player = Player(speed: 1.0, direction:.right) Mobile Application Development in ios 17

Classes (cont.) class FlyingPlayer : Player { var altitude: Float init (speed: Float, direction: Direction, altitude: Float) { self.altitude = altitude super.init (speed: speed, direction: direction) override func energize() { super.energize() altitude += 1.0 Must initialize all nonoptional child properties before initializing parent. var flyingplayer = FlyingPlayer(speed: 1.0, direction:.right, altitude: 1.0) Mobile Application Development in ios 18

Class vs. Struct Classes passed by reference Structs passed by value class Foo1 { var x : Int = 1 func changex (foo : Foo1) { foo.x = 2 struct Foo2 { var x : Int = 1 func changex (foo: Foo2) { foo.x = 2 // error var tmpfoo: Foo2 = foo tmpfoo.x = 2 var foo1 = Foo1() changex(foo: foo1) foo1.x // equals 2 var foo2 = Foo2() changex(foo: foo2) foo2.x // equals 1 Mobile Application Development in ios 19

Optional Chaining var myplayer = Player(speed: 1.0, direction:.right) let firstitem = myplayer.inventory.first // error let firstitem = myplayer.inventory!.first// error let firstitem = myplayer.inventory?.first// nil (OC) myplayer.inventory?.append("potion") // nil (OC: no effect) type(of: firstitem) // Optional<String> if let item = myplayer.inventory?.first { print("item = \(item)") // nothing printed (OC) myplayer.inventory = [] myplayer.inventory?.append("potion") let item = myplayer.inventory?.first // array initialized // potion added // potion if let item = myplayer.inventory?.first { print("item = \(item)") // item = potion Mobile Application Development in ios 20

Error Handling Do-try-throw-catch error handling enum myerror : Error { case good case bad case fatal func throwserror () throws { throw myerror.fatal func testerror () { do { try throwserror() print("no error") catch myerror.fatal { print("fatal") catch { print("good or bad") Mobile Application Development in ios 21

Error Handling try?: returns nil if error thrown try!: assumes no error thrown (or exec error) guard <condition> else {throw or return Preferred to: if not <condition> {throw or return if let result = try? throwserror() { print("no error: result = \(result)") let forcedresult = try! throwserror() guard (amount > 0) else { throw myerror.bad Mobile Application Development in ios 22

Regular type casting Type Casting let x = 10 let xstr = String(x) // 10, xstr of type String let xstr2 = "\(x)" // 10 let ystr = "100" let y = Int(ystr) // 100, y of type Optional<Int> var arrayofanything: [Any] var arrayofanyclassinstances: [AnyObject] Downcasting (as?, as!) var playerarray = [Player]() playerarray.append(flyingplayer) playerarray.append(player) var fp : FlyingPlayer! fp = playerarray[0] as? FlyingPlayer fp = playerarray[1] as? FlyingPlayer fp = playerarray[1] as! FlyingPlayer // fp = flyingplayer // fp = nil // error Mobile Application Development in ios 23

Protocols Required properties and methods Adopted by class, struct or enum type Said to conform to protocol protocol MyFunProtocol { func isfun() -> Bool class MyFunClass: MyFunProtocol { func isfun() -> Bool { return true Mobile Application Development in ios 24

Delegation Use protocols so class can delegate to others protocol MyFunDelegate { func isfun() -> Bool class MyFunDelegateClass: MyFunDelegate { func isfun() -> Bool { return true class MyFunClass { var delegate: MyFunDelegate? func fun() -> Bool { return delegate!.isfun() var myfunclass = MyFunClass() var myfunclassdelegate = MyFunDelegateClass() myfunclass.delegate = myfunclassdelegate myfunclass.fun() // returns true Mobile Application Development in ios 25

Resources Swift swift.org developer.apple.com/swift www.raywenderlich.com/category/swift Mobile Application Development in ios 26