Swift - 基本数据类型,及常/变量声明
(本文代码已升级至Swift4)
1,变量:使用var声明
var str:String = "hangge.com"
2,常量:使用let声明
let count:Int = 32
3,整形:
有符号整形:Int,Int8,Int16,Int32,Int64无符号整形:UInt,UInt8,UInt16,UInt32,UInt64
4,浮点型:
Float:32位浮点数类型Double:64位浮点数类型
5,布尔型:Bool
6,字符串:String
(1)判断是否为空:isEmptyvar str:String = "" if str.isEmpty{ print("空字符串") }(2)获取字符数量:string.characters.count
let str = "hangge.com" print("\(str.count)个字符")(3)检查字符串是否有特定前缀/后缀:hasPrefix/hasSuffix
var str = "hangge.com" if str.hasSuffix(".com"){ }(4)还可以用“\()”在字符串里包裹变量,常量
let name = "hangge.com" let msg = "欢迎来到 \(name)"(5)大小写转换:
可以通过字符串的uppercased()方法、lowercased()方法、capitalized属性来访问一个字符串的大写/小写/首字母大写
let str = "Welecome to hangge.com" var uppercase = str.uppercased() //WELECOME TO HANGGE.COM var lowercase = str.lowercased() //welecome to hangge.com var capitalized = str.capitalized //Welecome To Hangge.Com
7,字符:Character
var doller:Character = "$"
8,元组
let myProject = (one:"game",two:1234) print("\(myProject.one)")
string.characters.count已更新为string.count,另外大小写转换的那三个函数好像被弃用了。。。