๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

iOS/Swift

[3/18 - 3/22 TIL] Coursera - LearnQuestIntroduction to Programming in Swift 5

 

 

3/ 18

 

* Pow: ์ œ๊ณฑ 

  Sqrt: ๋ฃจํŠธ 

 

* ํ•˜๋‚˜๋Š” int ํ•˜๋‚˜๋Š” double ์ด๋ฉด ๊ณฑ์…ˆ์‹์œผ๋กœ ๋‘˜์„ ๊ณฑํ•  ๋•Œ ์„œ๋กœ ๋‹ฌ๋ผ์„œ ์—๋Ÿฌ๊ฐ€ ๋‚จ. ๊ทธ๋Ÿด ๋•Œ๋Š” ํ•˜๋‚˜๋ฅผ ๋‹ค๋ฅธ ํ•˜๋‚˜์— ๋งž์ถฐ์ฃผ๋ฉด ๋˜๋Š”๋ฐ, ์˜ˆ๋ฅผ ๋“ค์–ด Dluble(๋ณ€์ˆ˜๋ช…) ์ด๋Ÿฐ์‹์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋จ. 


 

์ฒ˜์Œ์œผ๋กœ Coursera ์—์„œ swift ๊ฐ•์˜๋ฅผ ๋“ค์—ˆ๋‹ค. ์•„์ง์€ ์™„์ „ ๊ธฐ์ดˆ๋‹จ๊ณ„๋ผ ์ง„๋„๊ฐ€ ์‰ฝ๊ฒŒ ์‰ฝ๊ฒŒ ๋„˜์–ด๊ฐ”๋‹ค. ์–ธ์–ด๊ฐ€ ๋‹ฌ๋ผ๋„ ํ”„๋กœ๊ทธ๋žจ์ด ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹์€ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€ ์•Š๋‹ค๋Š” ๊ฑธ ๋ชธ์†Œ ์‹ค๊ฐํ–ˆ๋‹ค.

 

 

3/19

 

var students = [String]() // initialize
var studentsList2: [string] // declare
students.append("John Adams")
print(students)

=> initialize ๋Š” ๋นˆ ๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด์ง€๋งŒ declare์€ ๋‹จ์ˆœ ๋ณ€์ˆ˜ ์„ ์–ธ

 

donutslist.insert("kiwi", at: 0)
donutsquantity.insert(0, at: 0)

=> ๋ฐฐ์—ด ๊ฐ€์žฅ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€ํ•  ๋•Œ insert(at:) ์‚ฌ์šฉํ•จ

 

for i in 0..<salaries.count {
    salaries[i] = salaries[i] + (salaries[i] * 0.10)
}

=> 0.. ์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•จ์„ ์˜๋ฏธํ•จ

 

Failed to prepare for communication with playground for an unknown reason

=> xcode ์—์„œ ์ด ์˜ค๋ฅ˜๊ฐ€ ๋œจ๋ฉด xcode ์ข…๋ฃŒ ํ›„ ๋‹ค์‹œ ์‹คํ–‰ํ•˜๋ฉด ํ•ด๊ฒฐ๋œ๋‹ค. 


์˜ค๋Š˜์€ swift์˜ ๋ฐฐ์—ด๊ณผ ๋ฃจํ”„์— ๋Œ€ํ•ด์„œ ๋ฐฐ์› ๋‹ค. ์ด ๋˜ํ•œ ๋‚ด๊ฐ€ ์ด๋ฏธ ๊ณต๋ถ€ํ–ˆ๋˜ javascript์™€ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€ ์•Š์•„ ์ˆ˜์›”ํ•˜๊ฒŒ ๊ณต๋ถ€ํ–ˆ๋‹ค. 

 

 

3/21

struct Person {
    let firstName: String
    let middleName: String?
    let lastName: String

    func printFullName() {
        // if middleName is nil use an empty string
        let middle = middleName ?? ""

        /* 
         if middleName == nil {
            middle = ""
         */

        print("\(firstName) \(middle) \(lastName)")
    }

}

var person1 = Person(firstName: "Jenna", middleName: nil, lastName: "Smith")
var person2 = Person(firstName: "Bob", middleName: "Leroy", lastName: "Jenkins")

person1.printFullName()
person2.printFullName()

 

* ์ƒˆ๋กœ ์•Œ๊ฒŒ ๋œ ๊ฐœ๋… 

=> String? : String ์ผ์ˆ˜๋„ nil ์ผ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ nil์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋„ ์žŠ์ง€ ๋ง๊ณ  ์ฒดํฌํ•ด์•ผ ํ•œ๋‹ค. 

 

class Person {
    let firstName: String
    let middleName: String?
    let lastName: String
    let spouse: Person?
    
    init(firstName: String, middleName: String?, lastName: String, spouse: Person?) {
        self.firstName = firstName
        self.middleName = middleName
        self.lastName = lastName
        self.spouse = spouse
    }
    
    func getFullName() -> String {
        let middle = middleName ?? "NA"
        return "\(firstName) \(middle) \(lastName)"
    }
    
}

let person = Person(firstName: "kimbo", middleName: "Joe", lastName: "Slice", spouse: nil)

if let spouseName = person.spouse?.getFullName() {
        print(spouseName)
} else {
    print("\(person.firstName) does not have a spouse")
}

// ์ค‘์š”ํ•˜๊ฒŒ ์ƒ๊ฐํ•ด์•ผ ํ•  3๊ฐ€์ง€
//1. If you have an optional (The Question Mark ?), make sure to check if it is nil
//2. Always use optional chaining
//3. Never force optionals, or you will get run time crashes.

 


์˜ค๋Š˜์€ optional ์ด๋ผ๋Š” ์ƒ์†Œํ•œ ๊ฐœ๋…์„ ๋ฐฐ์› ๋‹ค. ?๋ผ๋Š” ๊ธฐํ˜ธ๋กœ ์‚ฌ์šฉํ•˜๊ณ  nil์ด๋ผ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜ ๋˜ํ•œ ๋”ฐ์ ธ์•ผ ํ•˜๋Š” ์‹ ๊ธฐํ•œ ๊ฐœ๋…์ด์—ˆ๋‹ค. ์ฒ˜์Œ์—๋Š” ์ด๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐ์— ์–ด๋ ค์›€์„ ๋Š๊ปด ํ•œ๊ตญ์–ด๋กœ optional ๊ฐœ๋…์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฝ๊ณ  ๋‹ค์‹œ ๊ฐ•์˜๋ฅผ ์ฒญ์ทจํ•˜๋‹ˆ ์กฐ๊ธˆ์”ฉ ์ดํ•ด๊ฐ€ ๋˜๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค. ์‚ฌ์‹ค ๊ทธ๋ž˜๋„ ์•„์ง optional์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ์™„๋ฒฝํ•˜์ง€๋Š” ์•Š๋‹ค. ๊ทธ๋ž˜๋„ ์ด ๊ฐœ๋…์˜ ํฐ ํ‹€์„ ์ดํ•ดํ•˜๊ฒŒ ๋œ ๊ฒƒ ๊ฐ™์•„ ๋ฟŒ๋“ฏํ–ˆ๋‹ค. 

 

 

3/22

 

  • Objects and Classes (๊ฐ์ฒด์™€ ํด๋ž˜์Šค)
class Vehicle {
    // ์†์„ฑ, ๋ฐ์ดํ„ฐ, ์ƒํƒœ
    var tires = 4
    var headlight = 2
    var horsepower = 468
    var model = ""
    
    // ํ–‰์œ„, ๋ฉ”์„œ๋“œ, ๋™์ž‘
    func drive(){
        //accelerate the vehicle
        
    }
    func  brake() {
        
    }
}

// ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋กœ ๋‹ค์–‘ํ•œ ๊ฐ์ฒด ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ
let bmw = Vehicle()
bmw.model = "328i"

let ford = Vehicle()
ford.model = "F150"
ford.brake()

func passByReference (vehicle: Vehicle) {
    vehicle.model = "Cheese"
}

print(ford.model) // output: "F150" , Pass by value(ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ์— ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ณต์‚ฌํ•˜์—ฌ ์ „๋‹ฌํ•œ๋‹ค)
passByReference(vehicle: ford) //Pass by reference(์ธ์ž๋กœ ๋ฐ›์€ ๊ฐ’์„ ๋ฐ”๊พธ๋ฉด ๋ฐ”๊นฅ์˜ ๊ฐ’๋„ ๋ฐ”๋€๋‹ค)
print(ford.model) // "Cheese"

 

=> class์—๋Š” ์†์„ฑ, ๋ฐ์ดํ„ฐ, ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ถ€๋ถ„๊ณผ ํ–‰์œ„, ๋ฉ”์„œ๋“œ, ๋™์ž‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ถ€๋ถ„์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. 

 

* Pass by value : ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ์— ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ณต์‚ฌํ•˜์—ฌ ์ „๋‹ฌํ•จ

* Pass by reference: ์ธ์ž๋กœ ๋ฐ›์€ ๊ฐ’์„ ๋ฐ”๊พธ๋ฉด ๋ฐ”๊นฅ์˜ ๊ฐ’๋„ ๋ฐ”๋€œ

 

+ ์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๋ธ”๋กœ๊ทธ (swift์—์„œ์˜ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ)

https://jinshine.github.io/2018/05/22/Swift/5.OOP%20in%20Swift/

 

var someonesAge = 20

func passByValue(age: Int) {
    let newAge = age
}

passByValue(age: someonesAge) // ๋™์ž‘ํ•˜์ง€ ์•Š์Œ

// int์™€ double ๊ฐ™์€ value๋Š” ๋ณต์‚ฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๊ฐ์ฒด๋Š” ๋ณต์‚ฌํ•  ์ˆ˜ ์—†๋‹ค.

 

  • Inheritance (์ƒ์†)
class Vehicle {
    var tires = 4
    var make: String?
    var model: String?
    var currentSpeed: Double = 0
    
    init() {
        print("I am the Parent")
    }
    func drive(speedIncrease: Double){
        currentSpeed += speedIncrease * 2
    }
    func brake(){
        
    }
}

// ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ์ฒด๋ฅผ ์ด๋ ‡๊ฒŒ ๋”ฐ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ
class SportsCar: Vehicle {
    
    // override: ์žฌ์ •์˜
    // initialization: ์ดˆ๊ธฐํ™”
    // intializers: ํŠน์ • ํƒ€์ž…์˜ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ํ˜ธ์ถœํ•˜๋ฉฐ, ์ธ์ž๊ฐ€ ์—†๋Š” ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ์™€ ์œ ์‚ฌํ•œ ํ˜•์‹
    // override init: ์„œ๋ธŒํด๋ž˜์‹ฑ์‹œ, ์ˆ˜ํผ ํด๋ž˜์Šค์™€ ๊ฐ™์€ ์ดˆ๊ธฐํ™” ์ด๋ฆ„์œผ๋กœ ํ•  ๋•Œ ์‚ฌ์šฉ(๋ถ€๋ชจ ํด๋ž˜์Šค์™€ ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ)
    override init(){
        // super.init(): ์—ฐ์‡„์ ์ธ ์ดˆ๊ธฐํ™”
        super.init()
        print("I am the child")
        make = "BMW"
        model = "3 series"
    }
    
    override func drive(speedIncrease: Double) {
        currentSpeed += speedIncrease * 3
    }
}

let car = SportsCar()

=> ์ƒ์†์€ '์ž์†: ๋ถ€๋ชจ' ํ˜•ํƒœ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉฐ ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์œผ๋ฉฐ ์žฌ์ •์˜์™€ ์ดˆ๊ธฐํ™”๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ถ€๋ชจ ํด๋ž˜์Šค์™€ ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. 

 

 

  • Polymorphism (๋‹คํ˜•์„ฑ)

: polymorphicํ•œ ์‹œ์Šคํ…œ์˜ ๊ฒฝ์šฐ ๋ฒ”์šฉ์ ์ธ ๋ฉ”์†Œ๋“œ ์ด๋ฆ„์„ ์ •์˜ํ•˜์—ฌ ํ˜•ํƒœ์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ๋ณ€ํ™˜ ๋ฐฉ์‹์„ ์ •์˜ํ•ด ๋‘˜ ์ˆ˜ ์žˆ๋‹ค.

 

class Shape {
    var area: Double?
    
    func calculateArea(valA: Double, valB: Double){
        
    }
}

class Triangle: Shape {
    
    override func calculateArea(valA: Double, valB: Double) {
        area = (valA * valB) / 2
    }
}

class Rectangle: Shape {
    override func calculateArea(valA: Double, valB: Double) {
        area = valA * valB
    }
}

=> ์ƒ์†์„ ์ด์šฉํ•˜๋ฉด ์ฝ”๋“œ๋ฅผ ์ข€ ๋” ๊น”๋”ํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

  • MVC (Model - View - Controller)

=> Model layer: ์ฒญ์‚ฌ์ง„ 

=> View layer: ์Šคํฌ๋ฆฐ์—์„œ ์–ด๋–ป๊ฒŒ ๋ณด์ผ์ง€ (watchface)

=> Controller layer: view๋ฅผ ์กฐ์ข… (watchmaker)

 

* xcode - ํ”„๋กœ์ ํŠธ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์ฐพ๊ธฐ ํž˜๋“ค์—ˆ๋˜ ๋ถ€๋ถ„ ์ •๋ฆฌ 

- view > Show Library : Object Library 

- Embed in View : Storyboard ๊ฐ€์žฅ ํ•˜๋‹จ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ์•„์ด์ฝ˜ 

- Editer > Assistant > Ctrl + ๋งˆ์šฐ์Šค์™ผ์ชฝ : ์ฝ”๋“œ ์—ฐ๊ฒฐ ๊ฐ€๋Šฅ (์˜ค๋ฅ˜ ๋‚˜๋Š” ๊ฒฝ์šฐ xcode ์ข…๋ฃŒ ํ›„ ๋‹ค์‹œ ์‹คํ–‰) 

 

 

 

์•„์ดํฐ์— ๊ฐ„๋‹จํ•œ ๋ฐ•์Šค์™€ ํ…์ŠคํŠธ ๋„์šฐ๋Š” ํ”„๋กœ์ ํŠธ 

 

 

 

 


์˜ค๋Š˜์€ swift์—์„œ์˜ ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ดํ•ด์™€ ๋”๋ถˆ์–ด ์ƒ์†๊ณผ ๋‹คํ˜•์„ฑ๊ณผ ๊ฐ™์€ swift๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋Œ€ํ‘œ์ ์ธ ํŠน์ง•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋‹ค. ํ”„๋ก ํŠธ์—”๋“œ ๊ณต๋ถ€๋ฅผ ํ•˜๋ฉด์„œ๋„ ๊ฐ์ฒด ์•  ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ์ž˜ ๋˜์ง€ ์•Š์•˜๋Š”๋ฐ, ๋‹ค๋ฅธ ๊ฑด ๋ชฐ๋ผ๋„ ์˜ค๋Š˜ ๊ฐ์ฒด์— ๋Œ€ํ•ด์„œ ์™„๋ฒฝํ•˜๊ฒŒ ์ดํ•ดํ•˜๊ฒŒ ๋œ ๊ฑฐ ๊ฐ™์•„ ๋ฟŒ๋“ฏํ–ˆ๋‹ค. ๋˜ ์ง์ ‘ xcode์— ์•„์ดํฐ์— ๋„์šฐ๋Š” ํ…์ŠคํŠธ์™€ ๋ฐ•์Šค๋ฅผ ๋งŒ๋“ค์–ด๋ณด๋ฉด์„œ application์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๊ณ  ๋งŒ๋“ค์–ด์ง€๋Š”์ง€ ์•Œ๊ฒŒ ๋˜์—ˆ๊ณ  MVC๋ผ๋Š” ์ค‘์š”ํ•œ ํŠน์ง•๋„ ์•Œ๊ฒŒ ๋˜์–ด ๊ฐ ๋ถ€๋ถ„์ด ์–ด๋–ค ์—ญํ• ์„ ํ•˜๋Š”์ง€์— ๋Œ€ํ•ด์„œ๋„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ์ฒ˜์Œ์œผ๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ณ , swift์— ๋Œ€ํ•ด ๋ฐฐ์›Œ๊ฐ€๋ฉด์„œ ์–ด๋ ค์› ๋˜ ๋ถ€๋ถ„๋„ ์žˆ์—ˆ๊ณ  ์•„์ง ์กฐ๊ธˆ ์•„๋ฆฌ๊นŒ๋ฆฌํ•œ ๋ถ€๋ถ„๋„ ์žˆ์ง€๋งŒ ์ด๋ฒˆ ๊ฐ•์˜๋ฅผ ํ†ตํ•ด swift์— ๋Œ€ํ•œ ์ „์ฒด์ ์ธ ํ‹€์„ ๋ฐฐ์šธ ์ˆ˜ ์žˆ์—ˆ๋‹ค๋Š” ๊ฑด ์ •๋ง ์ข‹์•˜๋‹ค.