Swift学习——Swift基础详解(二)

上节说了没有营养的变量和常量,这玩意,都差不多,自己稍微看下就好了



Integers    整型

整数就是整数了,没有小数,整数有符号(+,-,0)或者无符号(0,+)

Swift提供了8,16,32,64位的有符号和无符号的整数,命名使用C的方式,比如,8位无符号的整型UInt8,32位有符号的整型就是Int32



Integer Bounds    整型范围

可以使用min 和 max获取整数类型的最大值和最小值

let minValue = UInt8.min  // minValue is equal to 0, and is of type UInt8
let maxValue = UInt8.max  // maxValue is equal to 255, and is of type UInt8


Int   int类型

在编程的时候,你可能不需要规定整型的位数,这时你可以使用int,它代表的类型与你的平台有关

On a 32-bit platform, Int is the same size as Int32.
On a 64-bit platform, Int is the same size as Int64.
32位系统,int表示有符号32位整型,64位系统就代表有符号64位整型

如果你是嵌入式方向的,可能就需要其他类型的整型了,否则直接用int就行了

32位系统中的int代表的范围是[ -2,147,483,648     ,    2,147,483,647 ]



UInt   无符号的int类型

与int类型对应的,这个表示无符号的,int表示有符号的

On a 32-bit platform, UInt is the same size as UInt32.
On a 64-bit platform, UInt is the same size as UInt64.



Floating-Point Numbers    浮点型

还是那两种浮点数类型

Double represents a 64-bit floating-point number. Use it when floating-point values must be very large or particularly precise.
Float represents a 32-bit floating-point number. Use it when floating-point values do not require 64-bit precision.
Double有15位的小数的精准度,而Float 有6位小数的精准度



Type Safety and Type Inference    类型安全和类型推理

官方的书在下面主要就是解释了下为什么Swift语言那么的安全,编译器会代替你进行类型检查,对于没有赋初值的变量或者常量,Swift还会推理出你的变量类型(名侦探柯南啊?),Bla Bla 就这么多,想详细了解自己看英文原版吧

额,下面就说下Swift有多么的'智能':

let meaningOfLife = 42
// meaningOfLife is inferred to be of type Int
Swift这时候就会断定你的这个常量是int类型的

let pi = 3.14159
// pi is inferred to be of type Double
Swift这时候就会断定你的这个常量是Double类型的(它只会认为是Double,不会认为是Float,保持精度么,其实智能?呵呵)

下面这个您可不要眨眼了:

let anotherPi = 3 + 0.14159
// anotherPi is also inferred to be of type Double
你猜,Swift判断你的这个常量是什么类型的?对嘛,Double



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值