首页 > 代码库 > Swift学习笔记

Swift学习笔记

常量 :let product_constant = "iphone6"

变量 :var product_var = "ipad" //不需要声明变量类型,会根据右侧的值推导左侧变量的类型

可以多个变量定义在一起:var x1 = 30, x2 = "abc"

可以精确指定变量的类型: var x1:Int = 27 //指定x1为INT型

println(x1)//输出打印单个变量

println("x1=\(x1), x2=\(x2)")//输出打印多个变量

 

初始化字符串

var emptyString1 = ""

var emptyString2 = String()

var string3 = "abc"

var string4 = String("abc")

比较字符串

if(string3 == string4){

}

连接字符串

let str1 = "hello"

let str2 = "world"

var str3 = str1 + str2 //str3就等于"hello world"

str1 += str2 //str1就等于"hello world"

字符串大小写转换

let lowerStr = "abc"

let upperStr = lowerStr.uppercaseString//小写变大写

let lowerStr2 = upperStr.lowercaseString//大写变小写

 

元组常量和变量

let product1 = (20,"iphone6",5888)

let product_name = "iphone4s"

var product2 = (10,"\(product_name)",3888)

println(product1)//(20,iphone6,5888)

println(product2)//(10,iphone4s,3888)