如何 return 来自函数内 if let 语句的值?
How to return a value from an if let statement inside a function?
我正在自学 Swift 官方 Apple 课程书籍,不幸的是没有解决方案。我发现自己陷入了这个问题。
Write a function that will take the name of an item for purchase and
will return the cost of that item. In the body of the function, check
to see if the item is in stock by accessing it in the dictionary
stock. If it is, return the price of the item by accessing it in the
dictionary prices. If the item is out of stock, return nil. Call the
function and pass in a String that exists in the dictionaries below.
Print the return value.
这些是提供的词典。
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)
和 return nil
时,Xcode 给我一条错误消息:“函数中缺少 return...”。
所以我在最后添加了另一个 return Int(price)
行。但是 Xcode 说它无法在范围内找到它。
如何从 if-let
语句中获取 price
值?
即使我在函数内声明一个变量,然后在 switch
内用 price
设置它的新值,它的初始值仍然在 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
命令,即使它永远不会发生。
您只能 return 来自 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
}
}
这可能有帮助
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
}
}
问题是,xcode 不知道如何 return 如果 if-let 语句不起作用,这应该有所帮助:
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
我正在自学 Swift 官方 Apple 课程书籍,不幸的是没有解决方案。我发现自己陷入了这个问题。
Write a function that will take the name of an item for purchase and will return the cost of that item. In the body of the function, check to see if the item is in stock by accessing it in the dictionary stock. If it is, return the price of the item by accessing it in the dictionary prices. If the item is out of stock, return nil. Call the function and pass in a String that exists in the dictionaries below. Print the return value.
这些是提供的词典。
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)
和 return nil
时,Xcode 给我一条错误消息:“函数中缺少 return...”。
所以我在最后添加了另一个 return Int(price)
行。但是 Xcode 说它无法在范围内找到它。
如何从 if-let
语句中获取 price
值?
即使我在函数内声明一个变量,然后在 switch
内用 price
设置它的新值,它的初始值仍然在 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
命令,即使它永远不会发生。
您只能 return 来自 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
}
}
这可能有帮助
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
}
}
问题是,xcode 不知道如何 return 如果 if-let 语句不起作用,这应该有所帮助:
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