swift基礎-2

來源:互聯網
上載者:User

標籤:

一、基本運算子

        let a = 5

        var b = 10

        b = a

        if a = b{

            swift 中賦值運算子,並不將自身作為一個值進行返回,所以編譯不合法,幫開發人員避免錯誤,很人性化的語言

        }

二、數學運算子

        let dog:Character = "??"

        let cow:Character = "??"

        let dogCow = dog + cow

        println(dogCow)

        

        // swift 的算術運算子 不允許溢出,通過溢出運算子來選擇值的溢出情況(a& + b)

        // 餘數運算子  可以用於 浮點數 flost

        8 % 2.5  // equals 0.5

        // 一元減運算子  

        let three = 3

        let minusTree = -three

        let plusThree = -minusTree

        println(minusTree)

        println(plusThree)

        // 一元加運算子

三、範圍運算子

===  !== 兩個恒等運算子  檢測兩個對象引用是否來自於同一個對象執行個體

兩個範圍  

封閉(a...b)表示:a到b的值

for index in 1...5{

println()

}

半封閉

(a..b),包頭不包尾

        let names = ["anna","alex","bbsc","sdfs"]

        let count = names.count

        for i in 0...count-1{

            println("person \(i) is called \(names[i])")

        }

四、字元和字串

快速鍵:

option +Y = "Y"

emoji = control + commod +space

swift中的字串不是指標 是實際的值

初始化Null 字元串

var emptyString = ""

var anotherEmptyStr = String()

if emptyString.isEmpty{

 

}

var variableString = "horse"

variableString += " and carriage"

countElements 計算字串中的字元數量

let countStr = "hahahh"

println(countStr count is \(countElements(countStr))")

NSString 的length 是基於UTF-16編碼的數目  而不是基於Unicode

swift中字串並不一定佔用相同的記憶體空間。

字串比較

let someStr1 = "abc"

let someStr2 = "abc"

if someStr1 == someStr2{

 

}

swift中的字串 不是指標 是實際的值

首碼相等

let animals = ["食肉:老虎","食肉:獅子","食草:羊","食草:牛","食草:馬"]

var aCount = 0

for animal in animals{

if animal.hasPrefic("食肉"){

++aCount

}

 

}

println("這裡有\(aCount)頭食肉動物")

 

轉換字串大小寫

let normal = "could you help me"

let shoty = normal.uppercaseString  //大寫

let whispered = normal.lowercaseString //小寫

字串編碼 輸出 都是使用C語言的 print

.utf8  .utf16

unicode標量

.unicodeScalars

輸出時要放個空格

for scalar in dogString.unicodeScalars

{

print("\(scalar)" )

}

 

swift基礎-2

相關文章

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.