0

我正在从官方的 Apple 课程书籍中自己学习 Swift,不幸的是,这些书籍没有提供解决方案。我发现自己陷入了这个问题。

编写一个函数,该函数将获取要购买的商品的名称并返回该商品的成本。在函数的主体中,通过在字典 stock 中访问该项目来检查它是否有库存。如果是,则通过在字典价格中访问它来返回项目的价格。如果商品缺货,则返回零。调用该函数并传入一个存在于以下字典中的字符串。打印返回值。

这些是提供的字典。

var prices = ["Chips": 2.99, "Donuts": 1.89, "Juice": 3.99, "Apple": 0.50, "Banana": 0.25, "Broccoli": 0.99]
var stock = ["Chips": 4, "Donuts": 0, "Juice": 12, "Apple": 6, "Banana": 6, "Broccoli": 3]

问题的第一部分非常简单。这是我写的代码:

func itemInStock(item: String) -> Int? {
    
    if let availableItems = stock[item] {
        switch availableItems {
        case 1...Int.max:
            print("\(item) are available: \(availableItems) items.")
            if let price = prices[item] {
                print("\(item) cost \(price) USD per item.")
                return Int(price)
            }
            
        default:
            print("Sorry, \(item) are out of stock")
            return nil
        }
    
    }
    return Int(price)
}

没有-> Int?return线它工作正常。当我添加return Int(price)andreturn nil时,Xcode 给了我一个错误信息:“Missing return in function...”。

所以我return Int(price)在最后添加了另一行。但是 Xcode 说它在范围内找不到它。

如何price从内部if-let声明中获得该值?

即使我在函数内部声明了一个变量,然后在 内部设置了它的新值priceswitch它仍然会在switch.

似乎有一种非常简单的方法可以处理这些情况,但我在任何地方都找不到它们。我尝试使用不同的关键字进行搜索。

UPD感谢 Joakim Danielson,我设法获得了更简洁的代码:

func itemInStock_V2(item: String) -> Double? {
    if let availableItems = stock[item], availableItems > 0 {
            print("\(item) are available: \(availableItems) items.\n\(item) cost \(String(describing: prices[item])) USD per item.")
            return prices[item]
        } else {
            print("Sorry, \(item) are out of stock")
            return nil
    }
}

我不知道可以在if let声明后立即添加另一个条件。

事实证明,我的问题是我必须在代码的每个return结果之后编写命令,即使它永远不会发生。

4

3 回答 3

0

您只从 if 语句返回,从不返回。我认为它必须是这样的:

func itemInStock(item: String) -> Int? {

    if let availableItems = stock[item] {
        switch availableItems {
        case 1...Int.max:
            print("\(item) are available: \(availableItems) items.")
            if let price = prices[item] {
                print("\(item) cost \(price) USD per item.")
               return Int(price)
           }
        default:
            print("Sorry, \(item) are out of stock")
            return nil
        }

    } else {
       return nil
    }
}
于 2020-12-16T11:24:32.393 回答
0

这可能会有所帮助

func itemCost(item: String) -> Double? {
    if let available = stock[item] {
        if available == 0 {
            // none available
            return nil
        } else {
            // return the price
            return prices[item]
        }
    } else {
        // unknown item
        return nil
    }
}
于 2020-12-16T11:26:47.793 回答
0

问题是,如果 if-let 语句不起作用,xcode 不知道返回什么,这应该会有所帮助:

func itemInStock(item: String) -> Int? {
    if let availableItems = stock[item] {
    
        switch availableItems {
        case 1...Int.max:
            print("\(item) are available: \(availableItems) items.")
            if let price = prices[item] {
                print("\(item) cost \(price) USD per item.")
                return Int(price)
            } else {
                // in case (if let price = prices[item]) didn't work
                return nil
            }
            
        default:
            print("Sorry, \(item) are out of stock")
            return nil
        }
        
    } else {
        // in case (if let availableItems = stock[item]) didn't work
        return nil
    }
} 

如果您想立即摆脱 return nil 语句,也可以使用 guard-let

于 2020-12-16T11:56:40.213 回答