Swift學習——Swift基礎詳解(二)

來源:互聯網
上載者:User

標籤: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 UInt8let 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



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.