无法将 'String' 类型的 return 表达式转换为 return 类型 '(Int) -> String'
cannot convert return expression of type 'String' to return type '(Int) -> String'
正常工作功能:
func doSomeThing(productName : String , productPrice : Double) -> String {
return "good jobs"
}
我可能认为函数可以接受数量参数和 return 字符串
func doSomeThing(productName: String , productPrice: Double) -> (Int) -> String{
func totaPrice(quantity : Int) -> Double {
return Double(quantity) * productPrice
}
return "Product Name \(productName) each price is \(productPrice) , total price \(totaPrice)"
}
let totaPrice = doSomeThing(productName: "iPhone", productPrice: 649)
print(totaPrice(5))
print(totaPrice(3))
但低于错误抛出:
ERROR at line 14, col 11: cannot convert return expression of type 'String' to return type '(Int) -> String'
return "Product Name (productName) each price is (productPrice) , total price (totaPrice)"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
如何解决这个问题?
注意:我想做一些类似 Currying
的功能。
我认为问题在于您试图在一种方法中 return 两次。相反,只需让您的方法采用 3 个参数和 return 一个字符串。
func doSomeThing(productName : String , productPrice : Double, quantity : Int) -> String {
let total = productPrice * Double(quantity)
return "The total for \(productName) is \(total)"
}
doSomeThing(productName: "my product", productPrice: 1.99, quantity: 1)
你的函数头说你正在 return 一个接受 Int
和 return 一个 String
的闭包,所以这就是你的函数应该 return.
这是一个使用所有输入来构造 String
:
的示例
func doSomeThing(productName: String, productPrice: Double) -> (Int) -> String {
return { n in "\(n) \(productName)s cost \(productPrice * Double(n))" }
}
let iPhone = doSomeThing(productName: "iPhone", productPrice: 649)
print(iPhone(5))
print(iPhone(2))
输出:
5 iPhones cost 3245.0
2 iPhones cost 1298.0
正常工作功能:
func doSomeThing(productName : String , productPrice : Double) -> String {
return "good jobs"
}
我可能认为函数可以接受数量参数和 return 字符串
func doSomeThing(productName: String , productPrice: Double) -> (Int) -> String{
func totaPrice(quantity : Int) -> Double {
return Double(quantity) * productPrice
}
return "Product Name \(productName) each price is \(productPrice) , total price \(totaPrice)"
}
let totaPrice = doSomeThing(productName: "iPhone", productPrice: 649)
print(totaPrice(5))
print(totaPrice(3))
但低于错误抛出:
ERROR at line 14, col 11: cannot convert return expression of type 'String' to return type '(Int) -> String' return "Product Name (productName) each price is (productPrice) , total price (totaPrice)" ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
如何解决这个问题?
注意:我想做一些类似 Currying
的功能。
我认为问题在于您试图在一种方法中 return 两次。相反,只需让您的方法采用 3 个参数和 return 一个字符串。
func doSomeThing(productName : String , productPrice : Double, quantity : Int) -> String {
let total = productPrice * Double(quantity)
return "The total for \(productName) is \(total)"
}
doSomeThing(productName: "my product", productPrice: 1.99, quantity: 1)
你的函数头说你正在 return 一个接受 Int
和 return 一个 String
的闭包,所以这就是你的函数应该 return.
这是一个使用所有输入来构造 String
:
func doSomeThing(productName: String, productPrice: Double) -> (Int) -> String {
return { n in "\(n) \(productName)s cost \(productPrice * Double(n))" }
}
let iPhone = doSomeThing(productName: "iPhone", productPrice: 649)
print(iPhone(5))
print(iPhone(2))
输出:
5 iPhones cost 3245.0
2 iPhones cost 1298.0