Swift學習筆記

來源:互聯網
上載者:User

標籤:ar   c   學習   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)

 

 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.