nus ios swift talk

Post on 27-Aug-2014

869 Views

Category:

Software

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Talk on iOS Swift Basics and Syntax at NUS Institute of Systems Science, an iOS Dev Scout event

TRANSCRIPT

Introduction to SwiftSwift Syntax and Basics

Why Are You Here?

Why Are You Here?!

• New to Programming - but want to learn to write iPhone apps

Why Are You Here?!

• New to Programming

• Existing Programmer, no experience with iOS

Why Are You Here?!

• New to Programming

• Existing Programmer, no experience with iOS

• Experienced Obj-C iOS Developer, no experience with Swift

Why Are You Here?!

• New to Programming

• Existing Programmer, no experience with iOS

• Experienced Obj-C iOS Developer, no experience with Swift

• Already knows Swift

Why Are You Here?!

• New to Programming

• Existing Programmer, no experience with iOS

• Experienced Obj-C iOS Developer, no experience with Swift

• Already knows Swift - what are you doing here?

What is Swift, Anyway?

What is Swift, Anyway?!

Apple’s brand new programming language, that aims to make writing iOS apps easier, more flexible and fun.

About Me!

Co-Founder of Saleswhale, a B2B SaaS start-up

Mobile Intelligent Sales Productivityhttp://saleswhale.io

!

• Personally started iOS development 3 years ago

• Started Saleswhale, a mobile-first sales force management software, with beta customers as far as Silicon Valley

• Currently a team of 7 - all technical

• Bootstrapped from iOS consulting

Frequently Thought Questions

!

I’m a beginner. I want to learn to program iOS apps.

Should I learn Objective-C, Swift, or both?

Frequently Thought Questions

!

What is the big fuss about it?

Frequently Thought Questions

!

What is a playground?

Frequently Thought Questions

!

Will Swift give me superpowers?

What You Need• Xcode 6

A long, long time ago..#include <stdio.h>

int main() { printf(“hello, world!”); return 0;}

Today!

println(“Hello, World!”)

What You Will Learn• Basics of the Swift Language

• Playground

• Beyond the Basics

Variables!

var

Variables!

var whale

Variables!

var whale: String

Variables!

var whale: String = “Moby Dick”

Constants and Variables!

let whale: String = “Moby Dick”var weight: Double = 900.20let isBlue: Bool = true

Type Inference!

let whale = “Moby Dick”var weight = 900.20let isBlue = true

For Fun!

let whale = “Moby Dick”var weight = 900.20let isBlue = true let 🐮 = “bull”let 💩 = “poop”

For Fun!

let whale = “Moby Dick”var weight = 900.20let isBlue = true let 🐮 = “bull”let 💩 = “poop”let android = 🐮 + 💩

For Fun!

let whale = “Moby Dick”var weight = 900.20let isBlue = true let 🐮 = “bull”let 💩 = “poop”let android = 🐮 + 💩

Answer?

More String Power (String Interpolation)

More String Power (String Interpolation)

!

let price = 2, copies = 100000

More String Power (String Interpolation)

!

let price = 2, copies = 100000

let profit = “\(price) times \(copies) is \(price * copies)”

More String Power (String Interpolation)

!

let price = 2, copies = 100000

let profit = “\(price) times \(copies) is \(price * copies)”

Evaluate to?

Collection Types

Collection Types!

Arrays and Dictionaries

Collection Types!

What are Arrays?

Collection Types!

What are Arrays?!

A group of data!

Collection Types!

What are Arrays?!

A group of data!

100 DVD holder that sits on your floor and holds 100 dvds

Collection Types!

What are Dictionaries?!

Collection Types!

What are Dictionaries?!

Key-value pairs!

Collection Types!

What are Dictionaries?!

Key-value pairs!

{! “Godzilla” ! ! : “Romance”,! “Iron Man 2” !! : “Satire”,! “The Social Network”! : “Comedy”}

Array and Dictionary Literals

Array and Dictionary Literals!

Easiest way to create Arrays and Dictionaries

Array and Dictionary Literalsvar DVDs = [“Shawshank Redemption”, “The Godfather”, “Casablanca”]

Array and Dictionary Literalsvar DVDs = [“Shawshank Redemption”, “The Godfather”, “Casablanca”]

var genres = [“Godzilla” : “Romance, “Iron Man 2” : “Satire”, “The Social Network” : “Comedy”]

Array and Dictionary Literalsvar DVDs = [“Shawshank Redemption”, “The Godfather”, “Casablanca”]

var genres = [“Godzilla” : “Romance, “Iron Man 2” : “Satire”, “The Social Network” : “Comedy”]

Can work with other types

Loops

Loopswhile hazPetrol { drive()}

for var miles = 1; miles <= 10; ++miles { travel(miles)}

For-In

For-In: Rangesfor number in 1..5 { println(“\(number) times 2 is \(number * 2)”) }

For-In: Arraysfor movie in [“Godzilla”, “Iron Man”, “Jack”] { println(“\(movie) is a nice film.”) }

For-In: Dictionarylet dictionary = [“a” : 1, “b” : 2, “c” : 3]

for (key, value) in dictionary { println(“\(key) maps to \(value)”)}

Modifying an Arrayvar mugunthCars = [“BMW”, “Audi”, “VW”]

Modifying an Arrayvar mugunthCars = [“BMW”, “Audi”, “VW”]println(mugunthCars[2])

Modifying an Arrayvar mugunthCars = [“BMW”, “Audi”, “VW”]println(mugunthCars[2])mugunthCars += “Volvo”

Modifying an Arrayvar mugunthCars = [“BMW”, “Audi”, “VW”]println(mugunthCars[2])mugunthCars += “Volvo”mugunthCars += [“Toyota”, “Hyundai”, “Honda”]

Modifying an Arrayvar mugunthCars = [“BMW”, “Audi”, “VW”]println(mugunthCars[2])mugunthCars += “Volvo”mugunthCars += [“Toyota”, “Hyundai”, “Honda”]mugunthCars[0] = [“Proton”]

Modifying an Arrayvar mugunthCars = [“BMW”, “Audi”, “VW”]println(mugunthCars[2])mugunthCars += “Volvo”mugunthCars += [“Toyota”, “Hyundai”, “Honda”]mugunthCars[0] = [“Proton”]mugunthCars[3..5] = [“Lamborghini”, “Ferrari”]

Modifying a Dictionaryvar ratings = [“Apple” : “Good”, “Blackberry” : “Non Existent”]

Modifying a Dictionaryvar ratings = [“Apple” : “Good”, “Blackberry” : “Non Existent”]ratings[“Android”] = “Good”

Modifying a Dictionaryvar ratings = [“Apple” : “Good”, “Blackberry” : “Non Existent”]ratings[“Android”] = “Good”ratings[“Android”] = “Bad”

Short Break

Retrieving a Value from a Dictionary

let ratings = [“Apple” : “Good”, “Blackberry” : “Non Existent”]

let possibleRating = ratings[“Windows”]

Optionalslet ratings = [“Apple” : “Good”, “Blackberry” : “Non Existent”]

let possibleRating: String? = ratings[“Windows”]

Optionalslet ratings = [“Apple” : “Good”, “Blackberry” : “Non Existent”]

let possibleRating: String? = ratings[“Windows”]

if possibleRating == nil { println(“Rating for Windows not found”) }

Optionalslet ratings = [“Apple” : “Good”, “Blackberry” : “Non Existent”]

let possibleRating: String? = ratings[“Windows”]

if possibleRating == nil { println(“Rating for Windows not found”)} else { let windowsRating = possibleRating! println(“Rating for Windows is \(windowsRating)”}

Functionsfunc sayMeow() { println(“Meow!”)}

Functions and Parametersfunc sayMeow(name: String) { println(“Meow \(name)!”)}

Functions and Parametersfunc sayMeow(name: String) { println(“Meow \(name)!”)}

sayMeow(“Tom”)

!

!

!

Functions and Parametersfunc sayMeow(name: String) { println(“Meow \(name)!”)}

sayMeow(“Tom”)

!

!

!

Meow Tom!

Default Parameter Valuesfunc sayMeow(name: String = “Jerry”) { println(“Meow \(name)!”)}

sayMeow(“Tom”)

!

!

!

Meow Tom!

Returning Multiple Valuesfunc getDoubleDegree() -> (String, String) { // …after a few years… return (“Comp Sci”, “Electrical Engineering”)}

!

!

!

!

Tuples(3, 6, 9)

(42, “Meaning of Life”)

!

!

!

!

Tuples(3, 6, 9) // (Int, Int, Int)

(42, “Meaning of Life”) // (Int, String)

!

!

!

!

Using a Tuplefunc getDoubleDegree() -> (String, String) { // …after a few years… return (“Comp Sci”, “Electrical Engineering”)}

let (firstDegree, secondDegree) = getDoubleDegree()

println(“Got \(firstDegree) and \(secondDegree)”)

!

!

!

Classesclass Animal {

// properties // methods // initializers

}

Classesclass Animal {

// properties // methods // initializers

}

!

No header filesNo base class

Class Inheritanceclass Animal {

}

class Cow: Animal {

}

Propertiesclass Animal { var numberOfLegs = 0}

Propertiesclass Animal { var numberOfLegs = 0}

// Read-write property (var)

Propertiesclass Animal { var numberOfLegs = 0}

// Read-write property (var)// Constant (let)

Initialisingclass Animal { var numberOfLegs = 0}

let someAnimal = Animal()

Initialisingclass Animal { var numberOfLegs = 0}

let someAnimal = Animal()

// New instance of Animal

Dot Syntaxlet someAnimal = Animal()

println(someAnimal.numberOfLegs) // 0 legs

Dot Syntaxlet someAnimal = Animal()

println(someAnimal.numberOfLegs) // 0 legs

someAnimal.numberOfLegs = 5

println(someAnimal.numberOfLegs) // 5 legs

Class Initializationclass Animal { var numberOfLegs: Double init(numberOfLegs: Double) { self.numberOfLegs = numberOfLegs } } let someAnimal = Animal(numberOfLegs: 4)

Class Initializationclass Animal { var numberOfLegs: Double init(numberOfLegs: Double) { self.numberOfLegs = numberOfLegs } } let someAnimal = Animal(numberOfLegs: 4)

PracticeWrite a CPF Calculator program based on what you learnt that !

• Can calculate your CPF Contribution based on salary!

• Can dynamically adjust the contributed based on age!

• Calculates the total interest paid out in your OA, SA given a variable number of years

Thank You!

Gabriel Limgabriel@saleswhale.io

Mobile Intelligent Sales Productivityhttp://saleswhale.io

top related