所以我有一個類的方法isApplicableToList(list: [ShoppingItem]) -> Bool
。如果可以根據提供的產品ID列表應用折扣(即,產品必須與報價匹配)並且產品ID是901和902,則應該返回true
。如何返回布爾值?
我已經嘗試過但不確定是否完成正確或者如果有更好的方法。
在此先感謝!
class HalfPriceOffer :Offer {
init(){
super.init(name: "Half Price on Wine")
applicableProductIds = [901,902];
}
override func isApplicableToList(list: [ShoppingItem]) -> Bool {
//should return true if a dicount can be applied based on the supplied list of product ids (i.e. a product must be matched to an offer)
if true == 901 {
return true
}
if true == 902 {
return true
}
else {
return false
}
}
}
ShoppingItem
class ShoppingItem {
var name :String
var priceInPence :Int
var productId :Int
init(name:String, price:Int, productId:Int){
self.name = name
self.priceInPence = price
self.productId = productId
}
}
'true == 901'很可能不是你的意思。也許'productId == 901'? – danh
@danh我輸入其他內容時出現錯誤。 – Matt
如何定義ShoppingItem? – vacawama