Swift學(xué)習(xí)筆記

一 、在Playground中使用標(biāo)簽(在項(xiàng)目中未測試)

//:### 單行注釋------test
/*:
###  多行注釋 ----test
*/

在Xcode->Editor->Show Raw Markup

二、查看swift中類型

let  blinfgHeart = "\u{1F496}"
blinfgHeart.dynamicType//-->xx.dynamicType

三、打印不換行

for codeunit in blinfgHeart.utf8
{
//-------terminator:""
    print("\(codeunit)",terminator:"")
}

四、Nil Coalescing Operator

opt != nil ? opt! : b --->如果opt 為空 就讀取默認(rèn)值 否則就另外表達(dá)式的值

var userInput:String? 
let value = userInput ?? "a default input"http://value = "a default input"

var userInput:String? = "a user input"
let value = userInput ?? "a default input"http:// value = "a user input"

五、enumerate函數(shù)來遍歷數(shù)組

同時(shí)返回?cái)?shù)據(jù)項(xiàng)和索引值

for (index,Value) in fiveInts.enumerate()
{
    print("Index:\(index),Value:\(Value)")
}
打印結(jié)果:
**Index:0,Value:1**
**Index:1,Value:2**
**Index:2,Value:3**
**Index:3,Value:4**
**Index:4,Value:5**

六、字典與數(shù)組

字典數(shù)組是否為空

capotalNumber.isEmpty//返回BOOL

讓字典capotalNumber的key按照從小到大的排序

for number in capotalNumber.keys.sort()
{
    number
}
capotalNumber.keys

獲取到字典中key并依次放進(jìn)數(shù)組中

 let keyArray = [Int](capotalNumber.keys)

獲取到字典中value并依次放進(jìn)數(shù)組中

let valueArray = [String](capotalNumber.values)

七、Switch在swift上的修改

tuple

let point = (1,1)
switch point{
case (0,0):
    print("point is at the x-axis")
case(_,0):
    print("(\(point.0),0) is on the x-axis")
case(0,_):
    print("(0,\(point.1)) is on the y-axis")
case (-2...2,-2...2):
    print("(\(point.0),\(point.1) is inside the blue box")
default:
    print("(\(point.0),\(point.1) is outside the blue box")
    
}

Value binding

switch point{
case (let x,0):
    print("with the X value of:\(x)")
case (0,let y):
    print("with the Y value of:\(y)")
case (let x, let y):
    print("X:\(x)Y :\(y)")
}

switch point{
case let(x,y) where x == y:
    print("x==y")
case let(x,y) where x == -y:
print("x== -y")
case let(x,y):
    print("\(point.0,point.1)")

}

七、函數(shù)

_ 可以替換outName 讓函數(shù)調(diào)用跟C一樣

func multipleOfs(multiplier: Int,andValue:Int)
{
    print("\(multiplier)*\(andValue) = \(multiplier * andValue)")
}

multipleOfs(10,  andValue: 5)

func multipleOf(multiplier: Int,_ andValue:Int)
{
print("\(multiplier)*\(andValue) = \(multiplier * andValue)")
}

multipleOf(10,  5)

innername outname 讓函數(shù)調(diào)用更加易讀

func createTable(rowNumber row : Int,colNumber column: Int)
{
    print("Table:\(row) X \(column)")
}
createTable(rowNumber: 10, colNumber: 10)

intout param

func increment(inout vlaue: Int)
{
    vlaue += 1
}

var m = 10
increment(&m)
print("\(m)")//m ==11

八、復(fù)合函數(shù)和函數(shù)返回

var f4:(Int,Int) -> Int = mutipleOf//第七點(diǎn)用到的函數(shù)

fnparam是fn函數(shù)的參數(shù) ectecute調(diào)用名為fn的函數(shù)

func ectecute(fn:(String) -> Int? ,_ fnParam:String ){
    fn(fnParam)
}
-----------------------------------------
-----------------------------------------
func increment(n: Int) -> Int {
    return n+1
}

func decrement(n: Int) -> Int {
    return n - 1
}

typealias op = (Int) -> Int
func whichOne(n:Bool) -> op {
    return n ? increment : decrement
}

var one = 1
var oneToTen = whichOne( one < 10)

while one < 10
{
one = oneToTen(one)
}
 ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? 
typealias ops = (Int) -> Int
func whichOnes(n:Bool) -> ops {
    func increments(n: Int) -> Int {
        return n+1
    }
    
    func decrements(n: Int) -> Int {
        return n - 1
    }
    return n ? increment : decrement
}


九、匿名函數(shù)

//:### 標(biāo)準(zhǔn)初始化
//{}標(biāo)示 closure 全部內(nèi)容
var addClosure:(Int,Int) -> Int = {

    (a:Int ,b:Int) -> Int in
    return a + b
}
addClosure (5,10)

addClosure = {a,b in return a + b}

//: Single expression closure
addClosure = {a,b in a+b}

=================================
=================================
func execute(a:Int,_ b: Int,operation:(Int,Int)-> Int) -> Int {
    return operation(a,b)
}

func addFunc(a:Int ,_ b : Int) -> Int {
    return a + b
}

execute(1,10,operation:addFunc)
execute(1, 10, operation: addClosure)

execute(1, 10, operation: {(a:Int,b:Int)->Int in
    return a + b

})

execute(1, 10, operation: {a,b in a + b })

execute(1,10, operation: {$0 + $1})

execute(1, 10) {$1 + $0}//Tailing Closure
==================================
==================================
//:###Capturing value
//調(diào)用一個()空函數(shù) 返回Int 
func counting()->()->Int{

    var count1 = 0
    
    let incrementCount:()->Int = {
        count1++;
    }
    return incrementCount
    
}

十、謹(jǐn)慎處理的“空”值--Optional

nil 只能賦值給optional

var X:Int? 
var address :String?
var successRate:Double?

//判斷  converResult不為空 之后通過converResult!取值
if converResult != nil
{
    print( converResult!)
    let sum = converResult! + 1
}


if let numaber = converResult
{
    print(numaber)
}
else
{
    print("Convert result is nil")
}

if var number = converResult{
    number += 1
    print(number)
    print(converResult)
    let sum = number + 1
    
}
else{
    print("Conver result  is  nil")
}

Implicitly Unwrapped Optional

//: possibleString 一定不能為空
var possibleString :String! = "A dangerous way !"

print(possibleNumber)

possibleNumber + "use it with caution"

十一、Optional Chaining

enum Type{

    case CREDIT
    case DEPOSIT

}
class BankCard{

    var type:Type = .CREDIT

}
class  Person {
    var card = BankCard?()
    
    init(card:BankCard? = nil)
    {
        self.card = card
    }
}
let  nilPerson:Person? = nil
let  noCardPerson: Person? = Person()

let creditCardPrerson: Person? = Person(card : BankCard())
nilPerson?.card?.type
noCardPerson?.card?.type
creditCardPrerson?.card?.type

nilPerson.flatMap({$0.card}).flatMap({$0.type})
noCardPerson.flatMap({$0.card}).flatMap({$0.type})
creditCardPrerson.flatMap({$0.card}).flatMap({$0.type})

文章寫得不是很完整會持續(xù)更新,如果有缺陷的地方還請指出不勝感激。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容