Fresher Training—iOS Swift Day 4

by NhatHM
935 views

Today topic:

  • Encoding & Decoding Types
  • Asynchronous Closures & Memory Management
  • Value Types & Value Semantics
  • Protocol-Oriented Programming

Tham khảo: https://nhathm.com/swift-closure-escaping-autoclosure-b6cc22729e7

Exercises:

Exercise 01: ENCODING & DECODING

Make this source code Codeable

struct Student {
    var name: String
    var age: Int
    var study: [StudyClass]
}

struct StudyClass {
    var className: String
    var classCode: String
}

Điểm: 1

Exercise 02: ENCODING & DECODING

            Decoding this JSON

[{

         “country”: {

              “country_capital”: {

                  “capital_name”: “Ha Noi”,

                  “population”: 5000000

              },

              “country_name”: “Viet Nam”

         }

     },

     {

         “country”: {

              “country_capital”: {

                  “capital_name”: “Tokyo”,

                  “population”: 4000000

              },

              “country_name”: “Japan”

         }

     }

]

Điểm: 2

Exercise 03: MEMORY MANAGEMENT

    What wrong with below code? Fix it

class People {
    let name: String
    let email: String
    var bike: Bike?
    
    init(name: String, email: String) {
        self.name = name
        self.email = email
    }
    
    deinit {
        print("People deinit \(name)!")
    }
}

class Bike {
    let id: Int
    let type: String
    var owner: People?
    
    init(id: Int, type: String) {
        self.id = id
        self.type = type
    }
    
    deinit {
        print("Bike deinit \(type)!")
    }
}

var owner: People? = People(name: "NhatHM", email: "[email protected]")
var bike: Bike? = Bike(id: 1, type: "Honda")

owner?.bike = bike
bike?.owner = owner

owner = nil
bike = nil

Điểm: 2

Exercise 04: PROTOCOL

Making this source code runable

var color = UIColor.aliceBlue
color = UIColor.oceanBlue

Điểm: 2

Exercise 05: generics

What is generics? Show me an example

Điểm: 1

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.