私はメソッド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
}
}
「真== 901」はあなたが意味するものではない可能性があります。多分 'productId == 901' ?? – danh
@danh他に何かを入力するとエラーが発生します。 – Matt
'ShoppingItem'はどのように定義されていますか? – vacawama