Swift Generics控制台输出

时间:2014-09-21 12:14:39

标签: swift xcode6 swift-playground generics

在Playground中启用此类之后,我将丢失此类上面编写的所有控制台输出。这有什么问题?奇怪的是,它在switch语句中显示错误'Expected Pattern'一段时间,之后错误就消失了

class Cart<T> {
    var customerName: String
    var customerEmail: String
    var items: [T]
    var itemCount: Int {
        return items.count
    }
    var promoCode: String? = nil
    init(customerName: String, customerEmail: String){
        self.customerName = customerName
        self.customerEmail = customerEmail
        items = [T]()
    }

    func add (item: T) -> () {
        items.append(item)
    }

    func clear () -> () {
        items.removeAll(keepCapacity: false)
    }

    func remove (position: Int) -> () {
        items.removeAtIndex(position - 1)
    }

    func getPromoCodeDisplay () -> (String) {
        if let x = promoCode {
            return "Your promo code is \(x)"
        } else {
            return "You do not have a promo code"
        }
    }

    func getCartStatus () -> (String) {
        switch itemCount {
        case : 0
        return "You have no items in your cart."
        case : 1...3
        return "You have \(itemCount) items in your cart."
        default :
            return "You are an awesome customer!!"
        }
    }
}

1 个答案:

答案 0 :(得分:2)

你的冒号(:)在case语句中的位置错误。

func getCartStatus () -> (String) {
    switch itemCount {
    case 0 :
    return "You have no items in your cart."
    case 1...3 :
    return "You have \(itemCount) items in your cart."
    default :
        return "You are an awesome customer!!"
    }
}