數(shù)據(jù)基本類型
變量:使用var聲明
var str:String = "GlenRiver"
常量:使用let聲明
let count:Int = 5
整型
- 有符號(hào)整形:Int骗奖,Int8功炮,Int16全度,Int32,Int64
- 無符號(hào)整形:UInt鹦赎,UInt8,UInt16误堡,UInt32古话,UInt64
浮點(diǎn)型
- Float:32位浮點(diǎn)數(shù)類型
- Double:64位浮點(diǎn)數(shù)類型
布爾型
- Bool
字符串
- String
判斷是否為空 -- isEmpty
var str: String = ""
if str.isEmpty{
print("I'm a null string")
}
獲取字符數(shù)量 -- string.characters.count
let str = "GlenRiver"
print("\(str) has \(str.characters.count) characters")
檢查字符串是否有特定前綴/后綴 -- hasPrefix/hasSuffix
var str = "Chan.GlenRiver.exe"
if str.hasSuffix(".exe"){
print("\(str) has postfix '.exe'")
}
if str.hasPrefix("Chan."){
print("\(str) has prefix 'Chan.'")
}
還可以用“\()”在字符串里包裹變量,常量
let myName = "GlenRiver"
print("Welcome to \(myName) homepage")
大小寫轉(zhuǎn)換 -- uppercase()埂伦、lowercase()煞额、capitalized
let str = "Welcome to GlenRiver.com"
//swift3.0的用法
//WELCOME TO GLENRIVER.COM
var upperStr = str.uppercased()
//welcome to glenriver.com
var lowerStr = str.lowercased()
//Welcome To Glenriver.Com
var capitalizedStr = str.capitalized
//swift2.2的用法
//var upperStr = str.uppercaseString
//var lowerStr = str.lowercasedString
//var capitalizedStr = str.capitalizedString
字符
- Character
var doller:Character = "$"
元組
let myParam = (first:"game",second:1234)
print("The first element of tuple is \(myParam.first)")
print("The second element of tuple is \(myParam.second)")
轉(zhuǎn)載,請表明出處沾谜! GlenRiver
代碼下載:GR-Swift.rar
2016-2017 GlenRiver