70
Modern & Expressive or Magical Unicorn? ike Jones - @FlashGen Swift…

Swift - Modern & Expressive, or Magical Unicorn?

Embed Size (px)

Citation preview

Page 1: Swift  - Modern & Expressive, or Magical Unicorn?

Modern & Expressive or Magical Unicorn?

Mike Jones - @FlashGen

Swift…

Page 2: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

A point of reference, of sorts…

Page 3: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

Page 4: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

var myNamevar = “Mike Jones”:String ;

Page 5: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

var myName = “Mike Jones”

Page 6: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

var myName:String = “Mike Jones”;

var myName = “Mike Jones”

Page 7: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

var myName = “Mike Jones”

let myName = “Mike Jones”

Page 8: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

Loops

Page 9: Swift  - Modern & Expressive, or Magical Unicorn?

Loops

for var i = 0; i < 10; i++{

println(i)}

Page 10: Swift  - Modern & Expressive, or Magical Unicorn?

Loops

for i in 0..<10{

println(i)}

Page 11: Swift  - Modern & Expressive, or Magical Unicorn?

Loops

var myArray = [1,2,3,4,5]

for item in myArray{

println(item)}

Page 12: Swift  - Modern & Expressive, or Magical Unicorn?

Loops

var i = 1

while i < 10{

println(i)i++

}

Page 13: Swift  - Modern & Expressive, or Magical Unicorn?

Loopsvar i = 10

do {

println(i)i++

}while i < 10

Page 14: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

Conditionals

Page 15: Swift  - Modern & Expressive, or Magical Unicorn?

If…elsevar num = 10

if num < 20{

println(“less than 20")}else{

println("not less than 20")}

Page 16: Swift  - Modern & Expressive, or Magical Unicorn?

Switch…Casevar value = 10

switch value{

case 10:println("Yay! just right")

case 20:println("a bit too high")

default:println("Nothing to see here")

}

Page 17: Swift  - Modern & Expressive, or Magical Unicorn?

Switch…Casevar value = 6

switch value{

case 1...9:println("Too low")

case 10:println("Yay! just right")

case 20:println("a bit too high")

default:println("Nothing to see here")

}

Page 18: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

Optionals…

Page 19: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

Page 20: Swift  - Modern & Expressive, or Magical Unicorn?

Optionals

var optionalInteger: Int?

var optionalInteger: Optional<Int>

Page 21: Swift  - Modern & Expressive, or Magical Unicorn?

Optionals

var impUnwrappedString:String!

var impUnwrappedString:ImplicitlyUnwrappedOptional<String>

Page 22: Swift  - Modern & Expressive, or Magical Unicorn?

Optionals

var myString = “hello”

var myInt = myString.toInt()

Page 23: Swift  - Modern & Expressive, or Magical Unicorn?

Optionals

var myString = “hello”

var myInt = myString.toInt()

// myInt = nil

Page 24: Swift  - Modern & Expressive, or Magical Unicorn?

Optionals

var myString = “3”

var myInt = myString.toInt()

Page 25: Swift  - Modern & Expressive, or Magical Unicorn?

Optionals

var myString = “3”

var myInt = myString.toInt()

// myInt = 3

Page 26: Swift  - Modern & Expressive, or Magical Unicorn?

Optionals

var myString = “3”

var myInt:Int = myString.toInt()

Page 27: Swift  - Modern & Expressive, or Magical Unicorn?

Optionals

var myString = “3”

var myInt:Int = myString.toInt()

// Object of type Optional is not unwrapped

Page 28: Swift  - Modern & Expressive, or Magical Unicorn?

Optionals

var myString = “3”

var myInt:Int = myString.toInt()!

Page 29: Swift  - Modern & Expressive, or Magical Unicorn?

Optionals

var myString = “3”

var myInt:Int = myString.toInt()!

// myInt = 3

Page 30: Swift  - Modern & Expressive, or Magical Unicorn?

Optionals

var myString = "hello"

var myInt:Int = myString.toInt()!.advancedBy(5)

Page 31: Swift  - Modern & Expressive, or Magical Unicorn?

Optionals

var myString = "hello"

var myInt:Int = myString.toInt()!.advancedBy(5)

// This fails

Page 32: Swift  - Modern & Expressive, or Magical Unicorn?

Optionalsvar myString = "5"

var myOtherInt:Int = 0;

if let myInt = myString.toInt()?.advancedBy(5){

myOtherInt = myInt;}else{

println("It's nil")}

Page 33: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

Functions & Closures

Page 34: Swift  - Modern & Expressive, or Magical Unicorn?

Functions

func myFunc(valA:Int, valB:Int) -> Int{

return valA + valB}

var myInt = myFunc(4, 4);

Page 35: Swift  - Modern & Expressive, or Magical Unicorn?

Functions

func myFunc(#valA:Int, #valB:Int) -> Int{

return valA + valB}

var myInt = myFunc(valA:4, valB:4);

Page 36: Swift  - Modern & Expressive, or Magical Unicorn?

Functions

func myFunc(myAge valA:Int, yourAge valB:Int) -> Int{

return valA + valB}

var ourAgesCombined = myFunc(myAge:4, yourAge:4);

Page 37: Swift  - Modern & Expressive, or Magical Unicorn?

Tuples

func sayHello(greeting:String, name:String) -> (String, String){

return (greeting + " " + name, “Pleased to meet you :)”)}

var mygreeting = sayHello("Hello", "Reasons");

mygreeting.0; // Hello Reasonsmygreeting.1; // Pleased to meet you :)

Page 38: Swift  - Modern & Expressive, or Magical Unicorn?

Tuples

func sayHello(greeting:String, name:String) -> (fullGreeting:String, response:String)

{return (greeting + " " + name, “Pleased to meet you :)”)

}

var mygreeting = sayHello("Hello", "Reasons");

mygreeting.fullGreeting; // Hello Reasonsmygreeting.response; // Pleased to meet you :)

Page 39: Swift  - Modern & Expressive, or Magical Unicorn?

Tuples

func sayHello(greeting:String, name:String) -> (String, String){

return (greeting + " " + name, "Pleased to meet you :)")}

let (fullGreeting, response) = sayHello("Hello", "Reasons");

fullGreeting; // Hello Reasonsresponse; // Pleased to meet you :)

Page 40: Swift  - Modern & Expressive, or Magical Unicorn?

Tuples

func sayHello(greeting:String, name:String) -> (String, String){

return (greeting + " " + name, "Pleased to meet you :)")}

let (fullGreeting, _) = sayHello("Hello", "Reasons");

fullGreeting; // Hello Reasons

Page 41: Swift  - Modern & Expressive, or Magical Unicorn?

Variadic Functionsfunc addNumbers(numbers:Int...) -> Int{

var total = 0;

for num in numbers{

total += num}

return total;}

addNumbers(1,2,3,4,5) // 15addNumbers(1,2,3) // 6

Page 42: Swift  - Modern & Expressive, or Magical Unicorn?

Function Types

func myFunc(valA:Int, valB:Int) -> Int{

return valA + valB}

var myIntFunc :(Int, Int) -> Int = myFunc

myIntFunc(4,4)

Page 43: Swift  - Modern & Expressive, or Magical Unicorn?

Closures

var namesArray = ["Mike", "Jason", "Dennis", "Darren", "John"];

func sortAlphabetically (s1:String, s2:String) -> Bool{

return s1 < s2;}

let myFriends = sorted(namesArray, sortAlphabetically)

Page 44: Swift  - Modern & Expressive, or Magical Unicorn?

Closures

var namesArray = ["Mike", "Darren", "Dennis", "Jason", "Macca"];

let myFriends = sorted(namesArray, { (s1:String, s2:String) -> Bool in

return s1 < s2;})

Page 45: Swift  - Modern & Expressive, or Magical Unicorn?

in…out

var month = 9var year = 2015

func swapTwoInts(inout a: Int, inout b: Int) {

let tempA = aa = bb = tempA

}

swapTwoInts(&month, &year) // month = 2015, year = 9

Page 46: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

Structs, Classes, Protocols & Extensions

Page 47: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

Structs

Page 48: Swift  - Modern & Expressive, or Magical Unicorn?

Structsstruct Person{

var firstName: String = "Mike";var lastName: String = "Jones";

}

let myPerson = Person()

Page 49: Swift  - Modern & Expressive, or Magical Unicorn?

Classesclass Person{

var firstName: String;var lastName: String;

init(fName:String, lName:String){firstName = fName;lastName = lName;}

}

let myPerson = Person(fName:"Mike", lName:"Jones")

Page 50: Swift  - Modern & Expressive, or Magical Unicorn?

Classesclass Person{

var firstName: String;var lastName: String;

init(fName:String="John", lName:String="Smith"){firstName = fName;lastName = lName;}

}

let myPerson = Person(fName:"Mike", lName:”Jones”)// fName = "Mike", lName = "Jones" let myDefaultPerson = Person() // fName = "John", lName = "Smith"

Page 51: Swift  - Modern & Expressive, or Magical Unicorn?

Classesclass Person{

var firstName: String;var lastName: String;

init(fName:String, lName:String){firstName = fName;lastName = lName;}

convenience init(){self.init(fName: "John", lName: "Smith");}

}

let myPerson = Person() // fName = "John", lName = "Smith"

Page 52: Swift  - Modern & Expressive, or Magical Unicorn?

Classesclass Person{

var firstName: String;var lastName: String;

init(fName:String, lName:String){firstName = fName;lastName = lName;}

deinit {

// Clean up here}

}

Page 53: Swift  - Modern & Expressive, or Magical Unicorn?

Classesclass Person{

var firstName: String;var lastName: String;

var fullName:String{get{

return self.firstName + " " + self.lastName}}

// …}

Page 54: Swift  - Modern & Expressive, or Magical Unicorn?

Inheritanceclass Employee: Person{ var jobDescription :String = "I'm a developer"; func whatAreYouDoing() -> String { return "I'm working." }}

Page 55: Swift  - Modern & Expressive, or Magical Unicorn?

Inheritancepublic class Manager: Employee{ override var jobDescription :String { get { return super.jobDescription } set(value) { super.jobDescription = value; } } override func whatAreYouDoing() -> String { return "Very little - I'm the boss!" }}

Page 56: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

Protocols

Page 57: Swift  - Modern & Expressive, or Magical Unicorn?

Protocols

protocol UnicornsProt{

var unicornHerd:Int? { get set }

func howManyUnicornsDoYouOwn() -> Int?}

Page 58: Swift  - Modern & Expressive, or Magical Unicorn?

Protocols

class Person: UnicornsProt{

var unicornHerd: Int?;

func howManyUnicornsDoYouOwn() -> Int? {

return unicornHerd;}

}

Page 59: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

Extensions

Page 60: Swift  - Modern & Expressive, or Magical Unicorn?

Extensions

extension Person{

public var unicorns:String { return "UNICORNS!" }

public func moreUnicorns() -> String{return “MORE " + unicorns;}

}

Page 61: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

Generics…

Page 62: Swift  - Modern & Expressive, or Magical Unicorn?

Generics

var myArray :Array<String>

var myDict :Dictionary<String, Person>

var myOptionalInt :Optional<Int>

Page 63: Swift  - Modern & Expressive, or Magical Unicorn?

Genericsfunc swapTwoInts(inout a: Int, inout b: Int){

let tempA = aa = bb = tempA

}

func swapTwoStrings(inout a: String, inout b: String){

let tempA = aa = bb = tempA

}

func swapTwoDoubles(inout a: Double, inout b: Double){

let tempA = aa = bb = tempA

}

Page 64: Swift  - Modern & Expressive, or Magical Unicorn?

Genericsvar month = 9var year = 2015

var greeting = "Hello Reasons"var response = "Pleased to meet you :)"

func swapTwoValues<T>(inout a: T, inout b: T){

let tempA = aa = bb = tempA

}

swapTwoValues(&month, &year)// month = 2015, year = 9

swapTwoValues(&greeting, &response)// greeting = "Pleased to meet you :)// response = "Hello Reasons

Page 65: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

one more thing…

Page 66: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

Swift 2.0

Page 67: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

Open Source

Page 68: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

Apple Developer Program http://developer.apple.com

Page 69: Swift  - Modern & Expressive, or Magical Unicorn?

@flashgen | #reasonsto

Thank You

Page 70: Swift  - Modern & Expressive, or Magical Unicorn?

Mike Jonespixadecimal.comblog.flashgen.com@FlashGen