2015-01-08 14:59 发布:yuhang 浏览:434
下面是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,浮点型:
Float:32位浮点数类型
Double:64位浮点数类型
Float:32位浮点数类型
Double:64位浮点数类型
5,布尔型:Bool
6,字符串:String
(1)判断是否为空:isEmpty
1
2
3
|
var str: String
if str.isEmpty{
} |
(2)获取字符数量:countElements
1
2
|
let str = "hangge.com"
println( "\(countElements(str))个字符" )
|
(3)检查字符串是否有特定前缀/后缀:hasPrefix/hasSuffix
1
2
3
|
var str = "hangge.com"
if str.hasSuffix( ".com" ){
} |
(4)还可以用“\()”在字符串里包裹变量,常量
1
2
|
let name = "hangge.com"
let msg = "欢迎来到 \(name)"
|
(5)大小写转换:通过字符串的uppercaseString、lowercaseString、capitalizedString属性来访问一个字符串的大写/小写/首字母大写
1
2
3
4
5
|
let str = "Welecome to hangge.com"
var uppercase = str.uppercaseString //WELECOME TO HANGGE.COM
var lowercase = str.lowercaseString //welecome to hangge.com
var capitalized = str.capitalizedString //Welecome To Hangge.Com
|
7,字符:Character
1
|
var doller:Character = "$"
|
8,元组
1
2
|
let myProject=(one: "game" ,two: 1234 )
println( "\(myProjec.one)" )
|