Welcome to Swift

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

Introduction to Swift. Dr. Sarah Abraham

ios Application Development Lecture 2: Seminar and Unit 1

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

Swift. Introducing swift. Thomas Woodfin

COMP327 Mobile Computing Session: Lecture Set 1a - Swift Introduction and the Foundation Framework Part 2

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

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

ios Application Development Lecture 3: Unit 2

Xcode & Swift: Introduction

CSCE 110: Programming I

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

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

The Swift Programming Language

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

Lab1. Introduction to Python. Lab 4: Selection Statement. Eng. Mai Z. Alyazji

Brush up Python. December 20, 2018

Let's Look Back. We talked about how to create a form in HTML. Forms are one way to interact with users

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

ITP 342 Advanced Mobile App Dev. Memory

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

ITP 342 Mobile App Dev. Fundamentals

.Net Technologies. Components of.net Framework

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Functions and Collections. Dr. Sarah Abraham

Optimizing Swift Performance Session 409

lecture 11 Advanced Swift

CS ios. Extensions, Protocols, and Generics

Go vs. Swift: The Languages of The Modern Tech Giants

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Introduction to C++ with content from

Richard Mallion. Swift for Admins #TEAMSWIFT

CSC 1214: Object-Oriented Programming

Fundamentals of Programming

BEGINNING ios PROGRAMMING

SWIFT BASICS

SECTION II: LANGUAGE BASICS

CS 106 Introduction to Computer Science I

Fundamental of Programming (C)

Learn C# Errata. 3-9 The Nullable Types The Assignment Operators

Swift Programming THE BIG NERD RANCH GUIDE

6.096 Introduction to C++ January (IAP) 2009

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

lecture 1 hello, swift cs : spring 2018

CS 231 Data Structures and Algorithms Fall Binary Trees, Comparator Lecture 22 October 26, Prof. Zadia Codabux

Mobile Application Programming. Swift Classes

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

BCA-105 C Language What is C? History of C

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

4. Write the output that would be printed from each of the following code fragments. (6pts) a = 5 b = 4 temp = a a = b b = temp print(a, b, temp)

CS Kangmei Yang. Page 1

Unit 3. Operators. School of Science and Technology INTRODUCTION

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

ios Application Development Lecture 4: Navigation and Workflow

Understanding Undefined Behavior

Swift Programming PRE-COURSE WORKBOOK MIKEY WARD & ROBERT EDWARDS

Reviewing all Topics this term

DEPARTMENT OF MATHS, MJ COLLEGE

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

An Introduction to MATLAB

HACKING WITH SWIFT. Objective-C forple M A. Swift Developers S E. Paul Hudson

Finding Bugs Using Xcode Runtime Tools

C: How to Program. Week /Mar/05

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

iphone Application Programming Lecture 3: Swift Part 2

DATA 301 Introduction to Data Analytics Python. Dr. Ramon Lawrence University of British Columbia Okanagan

A flow chart is a graphical or symbolic representation of a process.

CSC 581: Mobile App Development Spring 2019

CS 11 java track: lecture 1

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

CS 1301 Exam 1 Answers Fall 2009

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

When Swift met classic algorithms and data structures. Caveats & Tips

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

Summary of Go Syntax /

Chapter 2 - Introduction to C Programming

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

Developing Applications for ios

Introduction to Programming

LEARNING ios APP DEVELOPMENT With Swift 3 & ios 10

CS 371L - Mobile Computing (ios) Dr. William C. Bulko. CS 371L Mobile Computing (ios) Introduction

2. C99 standard guarantees uniqueness of characters for internal names. A. 12 B. 26 C. 31 D. 48

Computer Components. Software{ User Programs. Operating System. Hardware

DigiPen Institute of Technology

CSC Web Programming. Introduction to JavaScript

Python 1: Intro! Max Dougherty Andrew Schmitt

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

4. Write the output that would be printed from each of the following code fragments. (8pts) x = 9 y = x / 2 print('y ==', y) +1 4.

Java for Python Programmers. Comparison of Python and Java Constructs Reading: L&C, App B

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Java Basic Programming Constructs

In Delphi script, when values are assigned to variables, the colon-equal operator is used; :=

Mobile Application Programming. Swift Classes

Introduction to C# Applications

Review of the C Programming Language for Principles of Operating Systems

Transcription:

Welcome to Swift http://activ8conf.jonathanhooper.net

What is Swift? A statically typed, object oriented programming language Programming language for ios, watchos, macos, and tvos Objective-C without the C Open source

Where do I get it? Xcode Swift Playgrounds *Linux Users: https://swift.org/download/#releases

Language Features

Constants and variables // variable var a = 5 // constant let b = 6 // type annotation let a: Int = 4 let c: String = "hello!"

Optionals var optionalstring: String? = "this could be nil. who knows? maybe?" optionalstring = nil // no worries here var nonoptionalstring: String = "this cannot be nil" nonoptionalstring = nil // this will explode

Optionals var optionalint: Int? = 5 optionalint! + 5 // no worries. returns 10 optionalint = nil optionalint! + 5 // this will explode.

Optionals var unwrappedoptionalint: Int! = 5 unwrappedoptionalint + 5 // no worries. returns 10 unwrappedoptionalint = nil unwrappedoptionalint + 5 // this will explode.

Comments // This is a single line comment /* This is a multiline comment. It spans many lines */

Documentation Comments /** This function doubles an integer */ func double(value: Int) -> Int { return value * 2 /// This function halves an integer func halve(value: Int) -> Int { return value / 2

Documentation Comments

Numbers let integer: Int = 5 let double: Double = 5.0 let unsignedinteger: UInt = 3 let int64bit: Int64 = 6 let int32bit: Int32 = 7 let float: Float = 2.7

Strings let helloworld = "Hello, world!" let characterh: Character = "H" let mrbond = "James Bond" let goodbyemrbond = "Goodbye, \(mrbond)"

Operators

Operators - Assignment var a = 5 // a now equals 5

Operators - Arithmetic 1 + 2 // equals 3 3-4 // equals -1 5 * 6 // equals 30 7.0 / 8.0 // equals 0.875 "hello, " + "world" // equals "hello, world"

Operators - Comparison 1 == 0 // false 2!= 1 // true 3 > 2 // true 4 < 3 // false 5 >= 4 // true 6 <= 5 // false

Operators - Logic!true // false true false // true true && false // false

Even more operators // The nil-coalescing operator a?? b // The closed-range operator 1...5 // The half-open range operator 1..<5 // The ternary conditional operator question? "It's true!" : "It's false!" // The remainder operator (different from a formal modulus operator, look it up!) 6 % 4 // The bitwise AND operator 0b11111100 & 0b00111111

Custom Operators infix operator <!!> extension Int { static func <!!>(a: Int, b: Int) -> Int { return (a + b) * 42 4 <!!> 5 // equal 378, or (4 + 5) * 42

Collections Arrays Dictionaries Sets...and more

Collections - Arrays var arrayofints = [1, 2, 3] var explicitarrayofints: [Int] = [4, 5, 6] var emptyarrayofints = [Int]() arrayofints[1] // returns 2

Collections - Arrays var arrayofints = [1, 2, 3] arrayofints.index(of: 3) // returns 2 arrayofints.max() // returns 3 arrayofints.min() // returns 1 arrayofints.append(4) // now equals [1, 2, 3, 4] arrayofints.count // returns 4 arrayofints.remove(at: 2) // now equals [1, 2, 4] arrayofints.first // returns 1 arrayofints.last // returns 4

Collections - Dictionaries var numbers = [ "one": 1, "two": 2, "three": 3, ] var emptydictionary = [String: Float]() var explicitdictionary: [Character: String] = [ "a": "ay", "b": "bee", "c": "see" ] numbers["one"] // returns 1

Collections - Dictionaries var numbers = [ "one": 1, "two": 2, "three": 3, ] numbers.count // 3 numbers.isempty // false numbers.removevalue(forkey: "two") // key/value for "two" removed numbers.updatevalue(4, forkey: "three") // numbers["three"] now equals 4 numbers.removeall() // numbers now equals [:]

Control Flow If/Else Guard While loops For loops For-in loops Switch Statements

If/Else var number = 5 if number == 5 { // do something

If/Else var number = 5 if number > 5 { // do something else { // do something else

If/Else var number = 5 if number > 5 { // do something else if number == 5 { // do something else else { // do something... elser...?

If/Else var optionalnumber: Int? = 5 if let optionalnumber = optionalnumber { // optionalnumber is now available as an unwrapped optional print(optionalnumber) else { // optionalnumber is nil print("optionalnumber is nil")

Guard func fibonacci(at number: Int) -> Int { guard number > 1 else { return number return fibonacci(at: number - 1) + fibonacci(at: number - 2) fibonacci(at: 7) // return 13

While loops var counter = 0 while counter < 5 { print(counter) counter += 1

For loops

For-in loops for number in [0, 1, 2, 3, 4] { print(number) for number in 0..<5 { print(number) for character in "01234".characters { print(character)

For-in loops [0, 1, 2, 3, 4].forEach { number in print(number) (0..<5).forEach { number in print(number) "01234".characters.forEach { character in print(character)

Switch statements var number = 3 switch number { case 0: print("zero") case 1: print("one") case 2: print("two") case 3: print("three") default: print("unrecognized number")

Functions func sayhello() { print("hello") sayhello() // prints "Hello"

Functions func sayhello(name: String) { print("hello, " + name) sayhello(name: "Chuck Norris") // prints "Hello, Chuck Norris"

Functions func sayhello(to name: String) { print("hello, " + name) sayhello(to: "Bruce Lee") // prints "Hello, Bruce Lee"

Functions func sayhello(_ name: String) { print("hello, " + name) sayhello("ip Man") // prints "Hello, Ip Man"

Functions func hellostring(for name: String) -> String { return "Hello, " + name let string = hellostring(for: "Kung Fury") print(string) // prints "Hello, Kung Fury"

Enums enum PrimateType { case Monkey case Gorilla case Human

Enums var primatetype = PrimateType.Gorilla switch primatetype { case.monkey: print("ooh ooh ahh ahh") case.gorilla: print("*gorilla noise*") case.human: print("lorem Ipsum")

Structs struct Primate { let name: String let type: PrimateType func makeanoise() { switch type { case.monkey: print("ooh ooh ahh ahh") case.gorilla: print("*gorilla noise*") case.human: print("lorem Ipsum")

Structs let harambe = Primate(name: "Harambe", type:.gorilla) print(harambe.name) // prints Harambe harambe.makeanoise() // prints *gorilla noise*

Classes

class Primate { Classes let name: String let type: PrimateType init(name: String, type: PrimateType) { self.name = name self.type = type func makeanoise() { switch type { case.monkey: print("ooh ooh ahh ahh") case.gorilla: print("*gorilla noise*") case.human: print("lorem Ipsum")

Classes let batman = Primate(name: "Christian Bale", type:.human) print(batman.name) // prints Christian Bale batman.makeanoise() // prints Lorem Ipsum

Structs Define properties to store data Define methods to add functionality Define initializers Classes Define properties to store data Define methods to add functionality Define initializers Inheritance Type casting Deinitialization

Classes & Structs - self keyword struct Primate { //... func firstname() -> String? { return self.name.components(separatedby: " ").first let batman = Primate(name: "Christian Bale", type:.human) if let name = batman.firstname() { print(name) // prints Christian else { print("batman doesn't have a name")

Demo

Where to go from here? Swift documentation: https://swift.org/documentation Apple s ios documentation: https://developer.apple.com/develop/ Stanford OCW - CS 193P: http://web.stanford.edu/class/cs193p Ray Wenderlich: https://www.raywenderlich.com/ NSScreencast: http://nsscreencast.com/

Questions?