Swift (45 Mins Version)

download Swift (45 Mins Version)

of 62

Transcript of Swift (45 Mins Version)

  • 8/10/2019 Swift (45 Mins Version)

    1/62

    SwiftiOS and OS XThe future of Programming

  • 8/10/2019 Swift (45 Mins Version)

    2/62

  • 8/10/2019 Swift (45 Mins Version)

    3/62

    vs

  • 8/10/2019 Swift (45 Mins Version)

    4/62

    Variables and Constants

    var actorName : String = Hodor

    let totalSeasonCount : Int = 4

    Type Inference

    var actorName = Hodor

    let totalSeasonCount = 4

  • 8/10/2019 Swift (45 Mins Version)

    5/62

    Variables and ConstantsString Interpolation

    let lovelyLady = Sansa Stark

    let age = 16

    let fact = \(lovelyLady) is \(age * 365) days old

    println(fact)

    Sansa Stark is 5840 days old

  • 8/10/2019 Swift (45 Mins Version)

    6/62

    for

    while

    do while

    for in

    For In: Ranges

    for x in 110{println(x)}

    1

    23

    .

    .

    10

  • 8/10/2019 Swift (45 Mins Version)

    7/62

    For In: Ranges

    for (name, distance)in distanceMap{

    println(\(name) travels \(distance) KMs everyday)}

    let distanceMap = [Laddy : 50, Pam : 3, Aish : 40]

    Laddy travels 50 KMs everydayPam travels 3 KMs everyday

    Aish travels 40 KMs everyday

  • 8/10/2019 Swift (45 Mins Version)

    8/62

    10OR

  • 8/10/2019 Swift (45 Mins Version)

    9/62

    let itemName : String? = getItemName(itemId)if itemName{

    }

    let itemId = DBG834723

    let itemNameForSure = itemName!println(itemNameForSure)

    Define an optional

    Query an optional

    Unwrap an optional

  • 8/10/2019 Swift (45 Mins Version)

    10/62

    let itemName : String? = getItemName(itemId)if letitemNameForSure = itemName {

    println(itemNameForSure)}

    let itemId = DBG834723

    Optional Binding

  • 8/10/2019 Swift (45 Mins Version)

    11/62

    if let itemNameForSure = getItemName(itemId){println(itemNameForSure)}

    let itemId = DBG834723

  • 8/10/2019 Swift (45 Mins Version)

    12/62

    Demo

  • 8/10/2019 Swift (45 Mins Version)

    13/62

    Product

    id : Stringname : String?contributionInfo: ContributionInfo?

    ContributionInfo

    type : String?contributor : Person?

    Person

    name : String?age : Int?

  • 8/10/2019 Swift (45 Mins Version)

    14/62

  • 8/10/2019 Swift (45 Mins Version)

    15/62

    let age : Int? = product.contribution?.contributor?.age

    Optional Chaining

  • 8/10/2019 Swift (45 Mins Version)

    16/62

    ClosuresFunctions andfunchelloFolks(){

    println(Hey there)}

    helloFolks()

    Hey there

    Functions: Introduction

  • 8/10/2019 Swift (45 Mins Version)

    17/62

    ClosuresFunctions andfunchelloFolksWithName(name: String){

    println(Hey \(name))}

    helloFolksWithName(Geeks)

    Hey Geeks

    Functions: Parameters

  • 8/10/2019 Swift (45 Mins Version)

    18/62

    ClosuresFunctions andfunc getMessage(name: String) -> String{

    return Hey \(name)}

    let message = getMessage(Ram)

    println(message)

    Hey Ram

    Functions: Returning Values

  • 8/10/2019 Swift (45 Mins Version)

    19/62

    ClosuresFunctions andlet messagePrinter = {

    println(I am a printer)}

    Closures: Introduction

    let messagePrinter: () -> () = {println(I am a printer)

    }

    func messagePrinter() -> (){println(I am a printer)

    }

  • 8/10/2019 Swift (45 Mins Version)

    20/62

    ClosuresFunctions andlet messagePrinter = {

    println(I am a printer)}

    Closures: Introduction

    let messagePrinter: () -> () = {println(I am a printer)

    }

    messagePrinter()

    I am a printer

  • 8/10/2019 Swift (45 Mins Version)

    21/62

    ClosuresFunctions andfunction repeat(count: Int, task: () -> ()){

    for i in 1count{task()

    }}

    Closures: Send as parameters

    repeat(3, {println(Hodor)

    })

    repeat(3){println(Hodor)

    }

  • 8/10/2019 Swift (45 Mins Version)

    22/62

    ClosuresFunctions andlet heightBuffer = [15, 10, 12, 9]

    Closures: Sort an array

    heightBuffer.sort({

    })

    println(heightBuffer)

    [9, 10, 12, 15]

    return a Bool in

  • 8/10/2019 Swift (45 Mins Version)

    23/62

  • 8/10/2019 Swift (45 Mins Version)

    24/62

    Classes

    classBook{

    }

    Stored Properties

    var name: String= Joker and Batman

  • 8/10/2019 Swift (45 Mins Version)

    25/62

    Classes

    classBook{

    var name: String = Joker and Batmanvar bookInfo : String{

    get{return Name of this book is \(name)

    }}

    }

    Computed Properties

  • 8/10/2019 Swift (45 Mins Version)

    26/62

    Classes

    classBook{

    var name: String = Joker and Batmanvar bookInfo : String{

    get{return Name of this book is \(name)

    }set{

    }}

    }

    Computed Properties

  • 8/10/2019 Swift (45 Mins Version)

    27/62

    Classes

    classBook{

    var name: String = Joker and Batmanvar bookInfo : String{

    get{return Name of this book is \(name)

    }}

    }

    Computed Properties

  • 8/10/2019 Swift (45 Mins Version)

    28/62

    Classes

    classBook{

    var name: String = Joker and Batmanvar bookInfo : String{

    return Name of this book is \(name)}

    }

    Computed Properties

  • 8/10/2019 Swift (45 Mins Version)

    29/62

    Classes

    classBook{

    var name: String = Joker and Batmanvar bookInfo : String{

    return Name of this book is \(name)}

    }

    Instantiation

    let myFavouriteBook = Book()Type Inference

  • 8/10/2019 Swift (45 Mins Version)

    30/62

  • 8/10/2019 Swift (45 Mins Version)

    31/62

    ClassesInheritance

    class Ebook : Book{

    var format = epubinit(name: String){

    super.init(name)}

    }

  • 8/10/2019 Swift (45 Mins Version)

    32/62

    ClassesOverriding Computed Properties

    class Ebook : Book{

    var format = epubinit(name: String){

    super.init(name)}

    overridevar bookInfo : String{}

    }

  • 8/10/2019 Swift (45 Mins Version)

    33/62

    ClassesOverriding Computed Properties

    class Ebook : Book{

    var format = epubinit(name: String){

    super.init(name)}

    overridevar bookInfo : String{return Name of this ebook is \(name) (\(format))

    }}

  • 8/10/2019 Swift (45 Mins Version)

    34/62

    ClassesOverriding Computed Properties

    let myFavouriteBook = Ebook(name: Harry Porter)

    println(myFavouriteBook.bookInfo)

    Name of this ebook is Harry Porter (epub)

  • 8/10/2019 Swift (45 Mins Version)

    35/62

    ClassesOverriding Stored Properties

    class Ebook : Book{

    var format = epubinit(name: String){

    super.init(name)}

    }

  • 8/10/2019 Swift (45 Mins Version)

    36/62

    ClassesOverriding Stored Properties

    class Ebook : Book{

    var format = epubinit(name: String){

    super.init(name)}

    overridevar name : String{

    }}

    willSet{/*newValue available here*/}didSet{/*oldValue available here*}

  • 8/10/2019 Swift (45 Mins Version)

    37/62

    ClassesFunctions

    class Ebook : Book{

    var format = epubinit(name: String){

    super.init(name)}

    func downloadFile() -> Bool{..

    return true}

    }

  • 8/10/2019 Swift (45 Mins Version)

    38/62

  • 8/10/2019 Swift (45 Mins Version)

    39/62

    StructuresDefault Initializers

    var origin = Point(x: 0.0, y: 0.0)

    var size = Size(width: 100, height: 200)

  • 8/10/2019 Swift (45 Mins Version)

    40/62

    StructuresComputed Properties

    structSize{

    var width, height : Double}

  • 8/10/2019 Swift (45 Mins Version)

    41/62

    StructuresComputed Properties

    structSize{

    var width, height : Doublevar area : Double{

    return width*height}

    }

  • 8/10/2019 Swift (45 Mins Version)

    42/62

    StructuresFunctions

    structSize{

    var width, height : Doublevar area : Double{

    return width*height}

    }

  • 8/10/2019 Swift (45 Mins Version)

    43/62

    StructuresFunctions

    structSize{

    var width, height : Doublevar area : Double{

    return width*height}

    void isBiggerThanSize(other : Size) -> Bool{return self.area > other.area

    }}

  • 8/10/2019 Swift (45 Mins Version)

    44/62

    Demo

  • 8/10/2019 Swift (45 Mins Version)

    45/62

  • 8/10/2019 Swift (45 Mins Version)

    46/62

    EnumerationsRaw Values

    enumMonth : Int{

    case January = 1, February, March, April, June,July, August, September, October, November,December

    }

    let monthNumber = Month.March.toRaw()

  • 8/10/2019 Swift (45 Mins Version)

    47/62

    EnumerationsRaw Values

    enumSpecialCharacter : Character{

    case Tab = \tcase NewLine = \n

    }

  • 8/10/2019 Swift (45 Mins Version)

    48/62

    Enumerations

    enumTurn{

    case LeftTurn, RightTurn, UTurn}

    var currDirection = Turn.LeftTurn

    currDirection = .UTurn

  • 8/10/2019 Swift (45 Mins Version)

    49/62

    Enumerations

    enumExamStatus{

    case Fail

    case Pass(Int)}

    var status = ExamStatus.Fail

    status = .Pass(70)

    Associated Values

  • 8/10/2019 Swift (45 Mins Version)

    50/62

    func doIt(value : Int){

    switchvalue{case 1:{println(Its kinda hot in here)

    }

    case 2:{println(Its fun in here)

    }}

    }

    Pattern Matching : Integer

  • 8/10/2019 Swift (45 Mins Version)

    51/62

    func doIt(air : Int){ //air stands for All India Rank

    switch air{case 150:{println(Stands out)

    }

    case 51100:{println(Worthless. Jump from a tower)

    }}

    }

    Pattern Matching : Range

  • 8/10/2019 Swift (45 Mins Version)

    52/62

    func doIt(stat : ExamStatus){

    switch stat{case .Fail:{println(Dont show me your face again)

    }

    case .Pass(let marks):{println(Intelligent kid got \(marks) marks)

    }}

    }

    Pattern Matching : Associated Data

  • 8/10/2019 Swift (45 Mins Version)

    53/62

    func doIt(stat : ExamStatus){switch stat{

    case .Pass(100):{println(Best kid ever)

    }

    case .Pass(7099):{println(Study hard next time)

    }case .Pass(_):{

    println(Thank god you didnt fail)}

    }}

    Pattern Matching : Associated Data

  • 8/10/2019 Swift (45 Mins Version)

    54/62

    Demo

  • 8/10/2019 Swift (45 Mins Version)

    55/62

    GenericStructures and Classes

    struct Queue{

    var buffer = T[]()mutating func enqueue(element : T){buffer.append(element)

    }

    mutating func dequeue() -> T{

    return buffer.removeFirst()}

    }

  • 8/10/2019 Swift (45 Mins Version)

    56/62

    GenericFunctions

    func findIndex(needle : String, hay : String[]) -> Int?{

    for i=0; i

  • 8/10/2019 Swift (45 Mins Version)

    57/62

    GenericFunctions

    func findIndex(needle : T, hay : T[]) -> Int?{

    for i=0; i

  • 8/10/2019 Swift (45 Mins Version)

    58/62

  • 8/10/2019 Swift (45 Mins Version)

    59/62

    GenericFunctions : Protocol Constraints

    protocol Equatable{

    func == (first : Self, second : Self) -> Bool}

    struct Point : Equatable{

    var x, y : Double

    }

    func == (first : Point, second : Point) -> Bool{return (first.x == second.x && first.y == second.y)

    }

  • 8/10/2019 Swift (45 Mins Version)

    60/62

    GenericFunctions : Protocol Constraints

    func == (first : Point, second : Point) -> Bool{return first.value == second.value

    }

    func !=(first : T, second : T) -> Bool{

    return !(first == second)

    }

  • 8/10/2019 Swift (45 Mins Version)

    61/62

  • 8/10/2019 Swift (45 Mins Version)

    62/62

    ;