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