问题
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