๊ณต๋ถ€ ์—ฐ์Šต์žฅ :-)

TIL_20180205 ~ 20180209

|

2018.02.05 ~ 06

(๊ณ„์† ์žํŒ๊ธฐ ๋ฏธ์…˜ ์ฝ”๋”ฉ๋งŒ ํ–ˆ๋‹ค) TIL ์žํŒ๊ธฐ๋ฏธ์…˜ vendingMachine, stockController์ฑ…์ž„ ๋ถ„๋ฆฌ stock / showcase ๊ฐ์ฒด ์ƒ์„ฑ - BeverageDisplayStand ํ”„๋กœํ† ์ฝœ ๊ตฌํ˜„

  • vendingmachine ์—์„œ ์Œ๋ฃŒ๋ฅผ ์‚ฌ๋Š” ๋™์ž‘์„ ํ•˜๋Š” buy()๋ฉ”์†Œ๋“œ - stockController์—์„œ์˜ removeItem()
    • ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ๋ฐธ๋ฅ˜๊ฐ€ mutate๋˜์ง€ ์•Š๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Œ!
    • ๋”•์…”๋„ˆ๋ฆฌ์—์„œ key๊ฐ’์œผ๋กœ ์ ‘๊ทผํ• ๋–„ ๋ฐธ๋ฅ˜๊ฐ€ ์˜ต์…”๋„์ด๋ผ ๊ทธ๋Ÿฐ๊ฑด์ง€ ์•„๋ž˜์ฒ˜๋Ÿผ ์ผ๋ฐ˜ for๋ฌธ / if๋ฌธ์œผ๋กœ ์ ‘๊ทผํ•˜๋‹ˆ๊นŒ set.key == itemCode๋ถ€๋ถ„์—์„œ๋Š” ๋™์ž‘ํ–‡๋Š”๋ฐ ๋ฆฌํ„ด๋˜๋Š” self.stock์€ ๊ทธ๋Œ€๋กœ์˜€์Œ
// Before

mutating func removeItem(itemCode: ObjectIdentifier) -> [ObjectIdentifier: [Beverage]]{
       var tempStock = self.stock
       for var set in tempStock {
           if set.key == itemCode {
               historyLog.purchase.append(set.value[0])
               set.value.remove(at: 0)
           }
           if set.key != itemCode {
               print("์žฌ๊ณ ๊ฐ€ ์—†๋Š” ์Œ๋ฃŒ ์„ ํƒ Error")
           }
       }
       self.stock = tempStock
       return self.stock
   }

// After
mutating func buyItem(item: Beverage) -> [ObjectIdentifier: [Beverage]] {
        //var tempStock = self.stock
        var itemKey = ObjectIdentifier(type(of:item))
        for var set in self.stock {
            if set.key == ObjectIdentifier(type(of:item)) {
                itemKey = set.key
                break
//                let removedB = set.value.popLast()
//                print("๋ ˆ๋“œ๋ถˆ ์ œ๊ฑฐ์„ฑ๊ณต\(removedB!.description)")
//                print(Stock(sortedItems: self.stock).description)
            } else if set.key != ObjectIdentifier(type(of:item)) {
                print("์—†์Œ loop")
                //print(Stock(sortedItems: self.stock).description)
            }
        }
        self.stock[itemKey]!.removeLast()
        print(Stock(sortedItems: self.stock).description)
        //self.stock = tempStock
        return self.stock
    }
  • ์• ํ”„ํ„ฐ์ฝ”๋“œ์ฒ˜๋Ÿผ ObjectIdentifier๊ฐ€ ๋˜‘๊ฐ™์€ ํ‚ค๊ฐ’์„ ์ฐพ์•„์„œ ์ €์žฅํ•ด๋†“๊ณ  loop๋ฅผ ๋‹ค ๋ˆ ๋‹ค์Œ, ๋ฐ˜๋ณต๋ฌธ ๋ฐ–์—์„œ value์— ์ ‘๊ทผํ•˜๊ณ  ์˜ต์…”๋„์„ ํ’€์–ด์ค˜์•ผ ์ง€์›Œ์กŒ๋‹คโ€ฆ. ์ด๊ฑฐ๋•Œ๋ฌธ์— ๊ฑฐ์˜ ๋‘์‹œ๊ฐ„ ๋‚ ๋ฆผ ^^!ใ… ใ… ใ… 

2018.02.07 ~ 08

(๊ณ„์† ์žํŒ๊ธฐ ๋ฏธ์…˜ ์ฝ”๋”ฉ๋งŒ ํ–ˆ๋‹ค)

  • ์ด์ƒํ•œ enum InputView์—์„œ ์Œ๋ฃŒ์˜ต์…˜์„ 1๋ฒˆ์œผ๋กœ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด, Product์˜ rawValue๋กœ ๋งค์นญ. ๋ฐฉ๋ฒ•์€ enum๋‚ด์˜ matchKey()๋กœ int๊ฐ’์„ ๋„˜๊ฒจ์„œ ํ•ด๋‹นํ•˜๋Š” ํด๋ž˜์Šคํƒ€์ž…์˜ ObjectIdentifier๋ฅผ buy๋ฉ”์†Œ๋“œ์˜ ์ธ์ž๋กœ ๋„˜๊ธฐ๋ ค๊ณ  ํ–ˆ์Œ. ์•„๋ž˜์˜ ์ผ€์ด์Šค๋Š” VendingMachineํด๋ž˜์Šค ๋ฐ–์— enum์„ ์„ ์–ธํ•˜๊ณ  matchKey()๋ฅผ enum์•ˆ์— ์„ ์–ธํ•œ ์ƒํ™ฉ ```Swift // VendingMachine.swift

import Foundation

// Class ๋ฐ–์˜ enum enum Product: Int{ case ChocoMilk = 1 case BananaMilk case Coke case Coffee case DolceLatte case EnergyDrink

func matchKey(_ option: Int) -> ObjectIdentifier? {
    switch option {
    case 1: return ObjectIdentifier(type(of: Product.ChocoMilk))
    case 2: return ObjectIdentifier(type(of: Product.BananaMilk))
    case 3: return ObjectIdentifier(type(of: Product.Coke))
    case 4: return ObjectIdentifier(type(of: Product.Coffee))
    case 5: return ObjectIdentifier(type(of: Product.DolceLatte))
    case 6: return ObjectIdentifier(type(of: Product.EnergyDrink))
    default: return nil
    }
} }

// testCode // enum ๋‚ด์˜ matchKey()๋กœ ๋ฆฌํ„ด๋œ (์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์Œ๋ฃŒ์„ ํƒ ์˜ต์…˜์˜์— ์˜ํ•œ) key- ObjectIdentifier๊ฐ’๊ณผ ์žํŒ๊ธฐ๊ฐ€ ๋ณด์œ ํ•œ stock๋‚ด์˜ key-ObjectIdentifier์˜ ๊ฐ’์ด ๊ฐ™์€์ง€ ๋น„๊ตํ•˜๊ณ ์‹ถ์—ˆ์Œ

func testMatchKey() { var stock = Beverage let energyDrink = EnergyDrink(brand: โ€œ๋ ˆ๋“œ๋ถˆโ€, weight: 350, price: 2000, name: โ€œ๋ ˆ๋“œ๋ถˆโ€, manufactured: โ€œ20171010โ€)

    for _ in 0..<2 {
        stock.append(energyDrink)
    }
    let vending = VendingMachine(stockItems: stock)
    let productkeys = vending.stock.keys

    let result = Product.matchKey(.EnergyDrink)
    // ์—ฌ๊ธฐ์„œ result๊ฐ€ function์ด์—ˆ์Œ......

    // 1. let result = Product.matchKey(1) ์ด๋Ÿฐ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋ ค๊ณ ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ. ๋Œ€์ฒด ์–ด๋–ค ๊ฒฝ์šฐ์ธ์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์Œ ใ… ใ… 
    // Error: Instance member 'matchKey' cannot be used on type 'Product'; did you mean to use a value of this type instead?
} ``` - case 2 ์œ„์˜ ์ผ€์ด์Šค์—์„œ๋Š” matchKey๋ฅผ ๋‚ด๊ฐ€ ์›ํ•˜๋Š”๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๊ฐ€ ์—†์—ˆ์Œ.(ํŒŒ๋ผ๋ฏธํ„ฐ์— Int๋ฅผ ์ธ์ž๋กœ ๋„˜๊ธฐ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ enum case์ค‘ ํ•˜๋‚˜๋ฅผ ๋„˜๊ฒจ์•ผ ํ–ˆ์Œ..)๊ทธ๋ฆฌ๊ณ  matchKey()๊ฐ€ Instance๋ฉค๋ฒ„๋ผ์„œ Product enum์—์„œ ์“ธ ์ˆ˜ ์—†๋‹ค๊ณ  ํ•จ. ๋”ฐ๋ผ์„œ ์•„๋ž˜์ฒ˜๋Ÿผ enum์„ ์•ˆ์— ๋„ฃ๊ณ (matchKey()๊ฐ€ ์ธ์Šคํ„ด์Šค ๋‚ด์— ์กด์žฌํ•ด์•ผ๋˜๋Š” ์ค„ ์•Œ๊ณ ) ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•˜๋‹ˆ ๋˜ ์—๋Ÿฌ๊ฐ€ ๋‚จ!... ๊ทธ๋ฆฌ๊ณ  ์ด๊ฒŒ ๋™์ž‘ํ–ˆ๋‹คํ•˜๋”๋ผ๋„ matchKey์˜ case๋ฌธ ์•ˆ์—์„œ ObjectIdentifier ํƒ€์ž… ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ `Product.ChocoMilk`๋กœ ์ด๋ ‡๊ฒŒ enumํƒ€์ž…๊นŒ์ง€ ์ •ํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด, ๊ณ„์† ambiguous์—๋Ÿฌ๊ฐ€ ๋‚จ. enum์•ˆ์—์„œ๋Š” `ChocoMilk.self`๊ฐ€ ์•ˆ๋จ
// ์ด๋ ‡๊ฒŒ enum์„ ์•ˆ์— ๋„ฃ์–ด๋ณด์•˜๋‹ค

class VendingMachine {

    enum Product: Int{
        case ChocoMilk = 1
        case BananaMilk
        case Coke
        case Coffee
        case DolceLatte
        case EnergyDrink

        func matchKey(_ option: Int) -> ObjectIdentifier? {
            switch option {
            case 1: return ObjectIdentifier(type(of: Product.ChocoMilk))
            case 2: return ObjectIdentifier(type(of: Product.BananaMilk))
            case 3: return ObjectIdentifier(type(of: Product.Coke))
            case 4: return ObjectIdentifier(type(of: Product.Coffee))
            case 5: return ObjectIdentifier(type(of: Product.DolceLatte))
            case 6: return ObjectIdentifier(type(of: Product.EnergyDrink))
            default: return nil
            }
        }
    }
}

// testCode

func testMatchKey() {
        var stock = [Beverage]()
        let energyDrink = EnergyDrink(brand: "๋ ˆ๋“œ๋ถˆ", weight: 350, price: 2000, name: "๋ ˆ๋“œ๋ถˆ", manufactured: "20171010")

        for _ in 0..<2 {
            stock.append(energyDrink)
        }
        let vending = VendingMachine(stockItems: stock)
        let productkeys = vending.stock.keys

        let result = VendingMachine.Product.matchKey(1)
    // error : Instance member 'matchKey' cannot be used on type 'VendingMachine.Product'; did you mean to use a value of this type instead?
    }

ใ… ใ… ใ… ใ… ใ… ํ›„ ์˜ค๋Š˜์€ ์ง„์งœ ์ด์ƒํ•œ ๋‚ ์ด๋‹ค. ์ง€๊ธˆ ๋ ˆ๋ฒจ2 ๋งˆ์ง€๋ง‰ ๋ฏธ์…˜์„ ํ˜ผ์ž์„œ ํ•ด๋ณด๊ณ ์žˆ๋Š”๋ฐ, ๊ผฌ์ด๋Š”๊ฒŒ ๋„ˆ๋ฌด ๋งŽ๊ณ  ์—ฌ๊ธฐ์„œ ์ด๋ ‡๊ฒŒ ํ•ด๋†“์œผ๋ฉด ์ €๊ธฐ์— ์ €๋ ‡๊ฒŒ์งœ์„œ ์—ฐ๊ฒฐํ•ด์•ผ์ง€! ํ•˜๊ณ  ์„ค๊ณ„ํ–ˆ๋Š”๋ฐ ์ƒ๊ฐ์น˜๋„ ๋ชปํ•œ ๊ณณ์—์„œ ๋” ๋ณต์žกํ•œ ๊ตฌํ˜„์ด ํ•„์š”ํ•˜๊ณ . ๋˜ ์–ด๋–ค๊ฑด ์˜ˆ์ „์— ๋‹ค ๋ฐฐ์šด๊ฑด๋ฐ๋„ ์ƒ๊ฐ์ด ์•ˆ๋‚˜์„œ ๋ฐ”๋กœ๋ฐ”๋กœ ์ ์šฉํ•˜๊ธฐ๊ฐ€ ํž˜๋“ค์—ˆ๋‹ค. ๊ฒ€์ƒ‰์„ ํ•ด๋„ ์†”๋ฃจ์…˜์ด ๋– ์˜ค๋ฅด์ง€ ์•Š์•˜๋‹ค. ์˜ค๋Š˜์€ ๋ญ”๊ฐ€ ์ฝ”๋”ฉํ•˜๋Š”๊ฒŒ ๋˜๊ฒŒ ๋ฒ„๊ฑฐ์› ๋‹ค.

์ฝ”๋“œ์Šค์ฟผ๋“œ์•ˆ์— ์žˆ์„๋•, ๋ชจ๋ฅด๋Š”๊ฒŒ ์žˆ์œผ๋ฉด ๋ฉค๋ฒ„๋“ค์ด๋‚˜ JK์—๊ฒŒ ์งˆ๋ฌธํ•ด์„œ ํ•ด๊ฒฐํ•ด์™”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹จ๊ณ„๋ณ„๋กœ PR์„ ๋ณด๋‚ด๋ฉด ๊ฐœ์„ ์‚ฌํ•ญ๊ณผ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ํžŒํŠธ๊ฐ€ ํ”ผ๋“œ๋ฐฑ์œผ๋กœ ์˜ค๋‹ˆ๊นŒ, ์ฐจ๊ทผ์ฐจ๊ทผํ•ด๋‚˜๊ฐ€๋Š” ๊ฒƒ ๊ฐ™์€ ๊ธฐ๋ถ„์ด ๋“ค์–ด์„œ ๋‚ด๊ฐ€ ๋‚ด์‹ค๋ ฅ๋Œ€๋กœ ํ•ด๋‚˜๊ฐ€๋Š”๊ตฌ๋‚˜ ๋ผ๋Š” ์ฐฉ๊ฐ์„ ํ–ˆ๋˜๊ฒƒ ๊ฐ™๋‹ค. ์—ฌํƒœ๊นŒ์ง€ ๊ทธ๋ ‡๊ฒŒ ํ•ด์™”๋˜๊ฒŒ ๊ณผ์—ฐ ๋‚ด๊ฐ€ ์•Œ๊ณ  ํ•œ๊ฑด๊ฐ€์‹ถ๋‹ค. ๋ˆ„๊ตฐ๊ฐ€์˜ ๋„์›€ ์—†์ด๋Š” ํ•ด๊ฒฐ์„ ์ž˜ ๋ชปํ•˜๋Š”๊ฒƒ ๊ฐ™์•„์„œ ์šฐ์šธํ•ด์กŒ๊ณ , ์˜ค๋Š˜์€ ๋™๋„ค ์Šค๋ฒ…์—์„œ ์นด์ฝ”๋ฅผ ํ–ˆ๋Š”๋ฐ ์Šคํƒ€๋ฒ…์Šค์—์„œ xcodeํ™”๋ฉด ๋ณด๋‹ค๊ฐ€ ์ง„์งœ ๋ˆˆ๋ฌผ์ด ๋‚˜์˜ค๋ ค๊ณ ํ–ˆ๋‹คใ… ใ… ใ… ใ…  ์ง€๊ธˆ ์Šค์œ„ํ”„ํŠธ๋กœ ๋ ˆ๋ฒจ2๋ฅผ 3๊ฐœ์›”๋™์•ˆ๋ฐฐ์› ๋Š”๋ฐ, ์›๋ž˜ ํ•˜๋˜ ํ‹€์—์„œ ํฌ๊ฒŒ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๋Š” ๋ฏธ์…˜๋„ ํ˜ผ์ž ํ•ด๊ฒฐ ๋ชปํ•˜๊ณ โ€ฆ ๋ˆ„๊ตฐ๊ฐ€์˜ ๊ฐ€์ด๋“œ๊ฐ€ ์—†์œผ๋ฉด ๋‚œ ์ง„์งœ ์•ˆ๋˜๋Š”๊ฑด๊ฐ€ ์‹ถ์–ด์„œ ๋„ˆ๋ฌด ์†์ƒํ–ˆ๋‹ค. ํ‰์†Œ๊ฐ™์•˜์œผ๋ฉด ์ง„์งœ ์ด๋Ÿฐ ๊ธ€ ๋„์ ์ด๊ณ  ์šฐ์šธํ•ดํ• ์‹œ๊ฐ„์— ํ•˜๋‚˜๋ผ๋„ ๋” ํ•ด๋ณด์žฃ! ํ•˜๊ณ  xcode์‹คํ–‰ํ•ด์„œ ๋˜๋Š”๊ฑฐ ์•ˆ๋˜๋Š”๊ฑฐ ๋‹ค ์จ๋ณด๊ณ  ๊ทธ๋žฌ์—ˆ๋Š”๋ฐ. ์˜ค๋Š˜์€ ๋ญ”๊ฐ€ ๊ทธ๋Ÿด ํž˜๋„ ์—†๋‚˜๋ณด๋‹คใ… ใ… ใ… 

2018.02.09

๋ฐฉํ•™๊ฒธ ๊ธฐ๋ถ„์ „ํ™˜ ๊ฒธ ์ž ์‹œ ์‰ฌ๋Ÿฌ ๋™ํ•ด๋กœใ… ใ… ..!

Comments