Missing return in a function expected to return 'Double?'

后端 未结 3 1528
后悔当初
后悔当初 2021-01-29 07:52

I\'m grinding through Apple\'s App Development with Swift book and I have been having a few issues with the Optionals sections.

I\'m getting stuck accessing a dictionary

3条回答
  •  臣服心动
    2021-01-29 08:41

    Your function can be shortened a hell of a lot by taking advantage of some of the tools that’s Swift gives you. Here, take a look, this does the same thing...

    func priceCheck(name: String) -> Double? {
        guard let stockAmount = stock[name],
            stockAmount > 0,
            let price = prices[name] else {
            return nil
        }
    
        return price
    }
    

提交回复
热议问题