如何返回布尔值?

时间:2016-12-04 15:31:59

标签: swift boolean

所以我有一个方法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
    }
}

1 个答案:

答案 0 :(得分:3)

循环浏览列表中的项目,并使用<script src="processing-1.3.6.min.js"></script> <script type="application/processing" data-processing-target="pjs"> int[] worldSaves = new int[1]; void setup() { size(200, 200); worldSaves[0] = year(); } void draw(){ background(0); text(worldSaves[0], 10, 10); } </script> <canvas id="pjs"> </canvas> 方法测试项目productId是否在applicableProductIds列表中。如果未找到,请返回contains

false