不推荐使用“ +”:Swift 3.1中不推荐使用混合类型的加法 [英] '+' is deprecated: Mixed-type addition is deprecated in Swift 3.1

查看:107
本文介绍了不推荐使用“ +”:Swift 3.1中不推荐使用混合类型的加法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我直接将整数值(即1,2,3等)与另一个整数变量相加时

When I'm directly adding an integer value(i.e: 1,2,3,etc) with another integer variable

let arr:Array = ["One","Two"]
var valueT:Int64 = 0
value = arr.count + 1 //in this line

我收到以下警告:

'+' is deprecated: Mixed-type addition is deprecated. Please use explicit type conversion.

我通过以下方式纠正了此警告

value = Int64(value + 1)

尽管它是固定的,但我想知道为什么它称为混合类型加法,因为我没有使用 ++ 。还有在Swift 3.1中解决警告的更好方法吗?

Though it is fixed but I wanna know why its called Mixed-type addition as I didn't use ++ . Also is there a better way to fix the warning in swift 3.1?

更新:

下图是警告的证据。我正在使用Xcode版本8.3(8E162)。

The following image is the proof of warning. I'm using Xcode Version 8.3 (8E162).

allROR 是一个数组。

推荐答案

编辑:要在您的代码中产生错误,应该是

To generate the error with your code it should be like

let value = 5
let result: Int64 = value + 1

现在您将收到警告


'+':不推荐使用混合类型加法。请使用显式类型转换。

'+' is deprecated: Mixed-type addition is deprecated. Please use explicit type conversion.

但是从值来看,警告似乎具有误导性 1 的类型为 Int ,因此其总和也为 Int ,因此您只需将结果转换为 Int64 即可,这就是您的工作,这是完全可以的。

But it is looks like warning is misleading, as of both value and 1 is of type Int so its summation also Int so you need to simply convert the result to Int64 and it is what you doing and that is perfectly ok.

let result: Int64 = Int64(value + 1)

这篇关于不推荐使用“ +”:Swift 3.1中不推荐使用混合类型的加法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆