数据基本类型
变量:使用var声明
var str:String = "GlenRiver"
常量:使用let声明
let count:Int = 5
整型
- 有符号整形:Int,Int8,Int16,Int32,Int64
- 无符号整形:UInt,UInt8,UInt16,UInt32,UInt64
浮点型
- Float:32位浮点数类型
- Double:64位浮点数类型
布尔型
- Bool
字符串
- String
判断是否为空 -- isEmpty
var str: String = ""
if str.isEmpty{
print("I'm a null string")
}
获取字符数量 -- 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")
大小写转换 -- 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)")
转载,请表明出处! GlenRiver
代码下载:GR-Swift.rar
2016-2017 GlenRiver