[英]Int vs Integer in Swift
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. 文档只引用Int
但Xcode 6自动完成只在我输入时给我Integer
。 I started using Integer
when porting code only to find that you have to cast between the two types. 我只是在移植代码时才开始使用Integer
来发现你必须在这两种类型之间进行转换。
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
An Int
is the type whilst an Integer
is a protocol it implements. Int
是类型,而Integer
是它实现的协议。
You should be using Int
in declarations, ie: 您应该在声明中使用Int
,即:
var num: Int = 5
which is also the type that's inferred for integer literals when a type isn't specified, ie: 这也是未指定类型时推断的整数文字的类型,即:
var num = 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.