[英]+ is unavailable: Please use explicit type conversions or Strideable methods for mixed-type arithmetics
I'm trying to learn Swift and am looking at an old Generic example that worked in Swift 2 我正在尝试学习Swift,并且正在研究一个在Swift 2中工作的旧的Generic示例
func increment<T: Strideable>(number: T) -> T {
return number + 1
}
Now in Swift 4 it complains 现在在Swift 4中,它抱怨道
'+' is unavailable: Please use explicit type conversions or Strideable methods for mixed-type arithmetics
Why am I getting this error and what I am doing wrong? 为什么我会收到此错误以及我做错了什么?
Rather than using the +
operator, you can simply use Strideable.advanced(by:)
. 您可以简单地使用Strideable.advanced(by:)
,而不是使用+
运算符。
func increment<T: Strideable>(number: T) -> T {
return number.advanced(by: 1)
}
increment(number: 2) //returns 3
increment(number: 2.5) //returns 3.5
In Swift 4 use 在Swift 4中使用
.advanced(by: 1) .advanced(by:1)
for this refer URl:- https://developer.apple.com/documentation/swift/strideable 为此请参考URl: - https://developer.apple.com/documentation/swift/strideable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.