Int vs Integer in Swift

拜拜、爱过 提交于 2020-01-01 07:54:28

问题


Does anyone know what the difference is between these two types?

The docs only refer to Int but Xcode 6 auto complete only gives me Integer when I type. I started using Integer when porting code only to find that you have to cast between the two types.

For instance the following code gives the error Could not find an overload for '+' that accepts the supplied arguments.

var number1 : Int = 5
var number2 : Integer = 10
number1 + number2

回答1:


An Int is the type whilst an Integer is a protocol it implements.

You should be using Int in declarations, i.e:

var num: Int = 5

which is also the type that's inferred for integer literals when a type isn't specified, i.e:

var num = 5


来源:https://stackoverflow.com/questions/24053432/int-vs-integer-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!