Swift中的Int vs Integer [英] Int vs Integer in Swift

查看:94
本文介绍了Swift中的Int vs Integer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道这两种类型之间有什么区别?

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

文档只引用 Int 但是当我输入时,Xcode 6自动完成只给我整数。我只是在移植代码时才开始使用 Integer 来查找你必须在两种类型之间进行转换。

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


推荐答案

Int 是类型,而整数是一个类型它实现的协议。

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

你应该在声明中使用 Int ,即:

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

这篇关于Swift中的Int vs Integer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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