无法将 'CountableClosedRange<Int>' 类型的 return 表达式转换为 Switch-case 中的 return 类型 'Int?'
Cannot convert return expression of type 'CountableClosedRange<Int>' to return type 'Int?' in Switch-case
当用户从 pickerview select 时,我正在尝试 return 值范围。
我使用了 switch-case 语句。
我的问题是,如何在 return 语句中 return 值范围?
这是我的代码。这是不正确的,因为我收到此错误,无法将 'CountableClosedRange' 类型的 return 表达式转换为 return 类型 'Int?'
private func price(from string: String) -> Int? {
switch string {
case "less than 100":
return 0 ... 100 // the error is here
case "500-100":
return 100 ... 500
case "1000-500":
return 500 ... 1000
case "3000-1000":
return 1000 ... 3000
case "5000-3000":
return 3000 ... 5000
case "larger than 5000":
return (I don't know)
case _:
return nil
}
}
我不明白你的 switch cases 应该有什么意义,但这虽然在我看来在概念上是荒谬的,但至少可以编译:
private func price(from string: String) -> CountableClosedRange<Int>? {
switch string {
case "less than 100":
return 0 ... 100 // the error is here
case "500-100":
return 100 ... 500
case "1000-500":
return 500 ... 1000
case "3000-1000":
return 1000 ... 3000
case "5000-3000":
return 3000 ... 5000
case "larger than 5000":
return 5000 ... Int.max
case _:
return nil
}
}
当用户从 pickerview select 时,我正在尝试 return 值范围。 我使用了 switch-case 语句。 我的问题是,如何在 return 语句中 return 值范围?
这是我的代码。这是不正确的,因为我收到此错误,无法将 'CountableClosedRange' 类型的 return 表达式转换为 return 类型 'Int?'
private func price(from string: String) -> Int? {
switch string {
case "less than 100":
return 0 ... 100 // the error is here
case "500-100":
return 100 ... 500
case "1000-500":
return 500 ... 1000
case "3000-1000":
return 1000 ... 3000
case "5000-3000":
return 3000 ... 5000
case "larger than 5000":
return (I don't know)
case _:
return nil
}
}
我不明白你的 switch cases 应该有什么意义,但这虽然在我看来在概念上是荒谬的,但至少可以编译:
private func price(from string: String) -> CountableClosedRange<Int>? {
switch string {
case "less than 100":
return 0 ... 100 // the error is here
case "500-100":
return 100 ... 500
case "1000-500":
return 500 ... 1000
case "3000-1000":
return 1000 ... 3000
case "5000-3000":
return 3000 ... 5000
case "larger than 5000":
return 5000 ... Int.max
case _:
return nil
}
}