简体   繁体   English

+不可用:请对混合型算术使用显式类型转换或Strideable方法

[英]+ 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.

相关问题 不推荐使用&#39;+&#39;:在Swift 3.1中不推荐使用混合类型添加 - '+' is deprecated: Mixed-type addition is deprecated in Swift 3.1 Swift编译器为什么无法找出混合类型数组中元素的类型? - Why can't the Swift compiler figure out the type of an element in a mixed-type array? 类型“ String.Index”不符合协议“ _Strideable” - Type 'String.Index' does not conform protocol '_Strideable' Swift类型转换 - Swift type conversions buttonWithType&#39;不可用:使用对象构造&#39;UIButton(type :) - buttonWithType' is unavailable: use object construction 'UIButton(type:) Swift调用静态方法:type(of:self)vs explicit class name - Swift calling static methods: type(of: self) vs explicit class name 类型`String.index(aka`string.CharacterView.index`)不符合协议_Strideable - Type `String.index (aka`string.CharacterView.index`)does not conform to protocol`_Strideable` &#39;字符不可用&#39;请直接使用字符串 - 'characters is unavailable' please use string directly 'init不可用:使用'withMemoryRebound(to:capacity:_)'临时查看内存作为另一种布局兼容类型 - 'init is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type &#39;characters&#39; 不可用:请直接使用 String 。 我有这个问题 - 'characters' is unavailable: Please use String directly . I have a problem with this
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM