加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift 数据类型(一)

发布时间:2020-12-14 06:44:56 所属栏目:百科 来源:网络整理
导读:整型 整型包含有符号和无符号整型,如下: - 有符号 Int8 :有符号8位整型,取值范围:(-128 ~~ 127) Int16 :有符号16位整型,取值范围:(-32768~~32767) Int32 :有符号32位整型,取值范围:( 2的 31次方~~2的31次方 -1) Int64 :有符号64位整型,取值范


整型

整型包含有符号和无符号整型,如下:

- 有符号

Int8 :有符号8位整型,取值范围:(-128 ~~ 127)
Int16 :有符号16位整型,取值范围:(-32768~~32767)
Int32 :有符号32位整型,取值范围:( 2的 31次方~~2的31次方 -1)
Int64 :有符号64位整型,取值范围: (2的 63次方~~2的63次方 -1)
Int :和平台相关

- 无符号

Int8 :有符号8位整型,取值范围:(0 ~~ 255)
Int16 :有符号16位整型,取值范围:(0~~65535)
Int32 :有符号32位整型,取值范围:(0~~2的32次方 -1)
Int64 :有符号64位整型,取值范围:(0~~2的64次方 -1)
Int :和平台相关

如下代码可以看到取值范围为多少

import UIKit


var a16 : Int16  = 0
a16 = Int16.max
a16 = Int16.min


var b16 : UInt16  = 0
b16 = UInt16.max
b16 = UInt16.min


结果为:

32767
-32768

65535
0


单精度和双精度类型

Float: 32位浮点型,
Double :64位浮点型(默认)

以下为swift 官方文档:

NOTE

Double has a precision of at least 15 decimal digits,whereas the precision of Float can be as little as 6 decimal digits. 
The appropriate floating-point type to use depends on the nature and range of values you need to work with in your code. 
In situations where either type would be appropriate,Double is preferred.



Double 类型取小数点后15位值,若小数点后 15 位以后还有值,按照 四舍五入计入第 15位。
Float 类型取小数点后6位值,若小数点后 6 位以后还有值,按照 四舍五入计入第 6位。

可以通过下面的示例验证:


var d :Float
d = 4.1234567

var e :Double
e = 2.123456789012345612312313


结果为:

4.123456
2.123456789012346

布尔类型

Swift has a basic Boolean type,called Bool.

Bool 类型比较简单,只有两个值 true 和 false ,一般做逻辑判断。

var c : Bool
c = false


参考资料

1,小码哥视频
2, swift 官方资料

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读