美文网首页
第一部分: 基础部分 ~ 闭包函数

第一部分: 基础部分 ~ 闭包函数

作者: wg689 | 来源:发表于2022-07-02 00:11 被阅读0次
import UIKit
import Darwin

var greeting = "Hello, playground"

let a = 10

// 10:57
let (c, b) = (10, 11)

//Swift 的赋值操作并不返回任何值。所以下面语句是无效的:

let f = 10 % 9

let three = 3

let minusThree = -three

let alsoThree = +three

var s = 2

s += 2

1 == 1

2 != 1

let name = "word"

if name == "word" {
    print("equal")
} else {
    print("\(name) not equal")
}


(1, "zebe") == (1, "zebe")
(1, "zebe") == (2, "zebe")

(2, "c") < (2, "d")



let contentHeight = 12

let hasHeader = false

var rowHeight = contentHeight

if hasHeader {
    rowHeight = rowHeight + 20
} else {
    rowHeight = rowHeight - 20
}

rowHeight = hasHeader ? rowHeight + 20 : rowHeight - 20


let defaultColorName = "red"

let userDefinedColorName: String? = ""

var colorNameToUse = userDefinedColorName ?? defaultColorName

for index in 1...5 {
    print("\(index) * 5 = \(index * 5) ")
}

// 闭区间
let names = ["11","22","33","44","55"]
let  count = names.count
for i in 0 ..< count {
    print("第\(i)个人的名字叫做 \(names[i])")
}


for name in names[2...] {
    print(name)
}

for name in names[...2] {
    print(name)
}



for name in names[..<2] {
    print(name)
}


let allowedEntry = false
if !allowedEntry {
    print("Aceess")
}

let enteredDoorCode = true
let passedRetinaScan = false

if enteredDoorCode && passedRetinaScan {
    print("welcome")
} else {
    print("ass denit")
}




if enteredDoorCode || passedRetinaScan {
    print("welcome")
} else {
    print("ass denit")
}


if (enteredDoorCode && passedRetinaScan) || hasHeader  {
    print("Welcome!")
} else {
    print("ACCESS DENIED")
}



// 字符串和字符
let someString = "Some string literal value"

var quatation = """
   Some string literal \
     Some string literal
     Some string literal
   Some string literal
"""
print(quatation)

quatation = """
Some string literal 2
"""
print(quatation)


// 字符串字面量的特殊字符

let wiseWords = "\"Imagination is more important than knowledge\" - Einston"

print(wiseWords)

var  dollarSign = "\u{24}"
print(dollarSign)

dollarSign = "\u{2665}"
print(dollarSign)

let threeDoubleQuotes = """
"Escaping the first quote"
"Escaping the first quote"\"\"
"""

print(threeDoubleQuotes)

//扩展字符串分隔符
let threeMoreDoubleQuotationMarks = #"""
Here are three more double quotes: """
"""#
print(threeMoreDoubleQuotationMarks)


var emptyString = ""
var emptyString2 = String()

if emptyString2.isEmpty {
    print("is emptyString")
}


var viriableString = "Horse"

viriableString +=  "and carriage"

for charactor in "Dog!" {
    print(charactor)
}


let string1 = "hello"
let string2 = "hello"
var welcome = string1 + string2

let exclamationMark: Character = "!"
welcome.append(exclamationMark)


let badStart = """
one
two

"""

let end = """
three
"""

print(badStart + end)


/// 字符串差值
let multiplier = 3
//// message 是 "3 times 2.5 is 7.5"

let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
print(message)

// 打印 "Write an interpolated string in Swift using \(multiplier)."

let Write =  #"Write an interpolated string in Swift using \(multiplier)"#

// 打印 "6 times 7 is 42."
print("6 times 7 is \(6*7)")


let eAcute: Character = "\u{E9}"
let combineEAcute: Character = "\u{65}\u{301}"


let precomposed: Character = "\u{D55C}"                  // 한
let decomposed: Character = "\u{1112}\u{1161}\u{11AB}"   // ᄒ, ᅡ, ᆫ
let decomposed2: Character = "\u{1112}\u{1161}\u{11AB}"   // ᄒ, ᅡ, ᆫ

let unusualMenagerie = "Koala , Snail , Penguin , Dromedary "

// 打印输出“unusualMenagerie has 40 characters”
print("unusualMenagerie has \(unusualMenagerie.count)characters")

var word = "cafe"

word += "\u{301}"    // 拼接一个重音,U+0301

print("the nume of charactor in \(word) is \(word.count)")


let greeting2 = "Guten Tag!"

greeting2[greeting2.startIndex]
greeting2[greeting2.index(before: greeting2.endIndex)]

greeting2[greeting2.index(after: greeting2.startIndex)]

let index = greeting.index(greeting.startIndex, offsetBy: 2)
greeting2[index]

let index2 = greeting2.startIndex
print(index2)


var welcome2 = "hello2"

welcome2.insert("!", at: welcome2.endIndex)

welcome2.insert(contentsOf: "three", at: welcome2.index(before: welcome2.endIndex))
            
print(welcome2)

welcome = "hello there"

welcome.remove(at: welcome.index(before: welcome.endIndex))
print(welcome)

let range = welcome.index(welcome.endIndex, offsetBy: -2)..<welcome.endIndex
welcome.removeSubrange(range)
print(welcome)

// 子字符串

greeting = "Hello, world!"

let index22 = greeting.firstIndex(of: ",") ?? greeting.endIndex
print(index22)
let beginning = greeting[..<index22]
print(beginning)


let newString = String(beginning)

let quotation = "We're a lot alike, you and I."
let someQuotation = "We're a lot alike, you and I."

if quatation == someQuotation {
    print("these two strings are considered equal")
}


let eAcuteQuestion = "Voulez-vous un caf\u{E9}?"
let combinedEAcuteQuestion = "Voulez-vous un caf\u{65}\u{301}?"

if eAcuteQuestion == combinedEAcuteQuestion {
    print("these two strings are considered equal")
}

let romeoAndJuliet = [
    "Act 1 Scene 1: Verona, A public place",
    "Act 1 Scene 2: Capulet's mansion",
    "Act 1 Scene 3: A room in Capulet's mansion",
    "Act 1 Scene 4: A street outside Capulet's mansion",
    "Act 1 Scene 5: The Great Hall in Capulet's mansion",
    "Act 2 Scene 1: Outside Capulet's mansion",
    "Act 2 Scene 2: Capulet's orchard",
    "Act 2 Scene 3: Outside Friar Lawrence's cell",
    "Act 2 Scene 4: A street in Verona",
    "Act 2 Scene 5: Capulet's mansion",
    "Act 2 Scene 6: Friar Lawrence's cell"
]

var act1SceneCount = 0
var act2SceneCount = 0

for scene in romeoAndJuliet {
    if scene.hasPrefix("Act 1") {
        act1SceneCount += 1
    } else if scene.hasPrefix("Act 2") {
        act2SceneCount += 1
    }
}

print("there is has \(act1SceneCount) count \(act2SceneCount) count")

let dogString = "Dog"
for codeUnit in dogString.utf8 {
    print("\(codeUnit)" ,terminator: " ")
}
print("")


for codeUnit in dogString.utf16 {
    print("\(codeUnit)" ,terminator: " ")
}
print("")


for scalar in dogString.unicodeScalars {
    print("\(scalar.value) ", terminator: "")
}
print("")
// 68 111 103 8252 128054

// 集合类型

var someInts: [Int] = []
print("someInts is one has\(someInts.count) 个元素")

someInts.append(3)

someInts = []


var threeDoubles = Array(repeating: 1.0, count: 3)
var threeDoubles2 = Array(repeating: 2.0, count: 3)
let  threeArr = threeDoubles + threeDoubles2
let  threeArr2 = threeDoubles2 + threeDoubles

var someIntsArr: [String] = ["egg","milk"]

var someIntsArr2 = ["egg","aee"]


if someIntsArr2.isEmpty {
    print("The shopping list is  empty")
} else {
    print("The shopping list is not empty")
}

someIntsArr2.append("tags")

var firstItem = someIntsArr2[0]

someIntsArr2[0] = "2333"

print(someIntsArr2)

someIntsArr2[1...2] = ["banana","apple"]
print(someIntsArr2)

someIntsArr2.insert("Mapple Syrup", at: 0)
print(someIntsArr2)

let mappleSyup = someIntsArr2.remove(at: 0)
print(someIntsArr2)

someIntsArr2.removeLast()
print(someIntsArr2)

for item in someIntsArr2 {
    print(item)
}


for (index, value) in someIntsArr2.enumerated() {
    print("item \(index) 的value \(value)")
}

var letters = Set<Character>()
print("letters is of type Set<Character> with \(letters.count) items.")

letters.insert("a")

var favoriteGenres: Set<String> = ["1","2","3"]


var favoriteGenres2: Set = ["11","2","3"]

if favoriteGenres2.isEmpty {
    print("favoriteGenres2 is empty")
}

if favoriteGenres2.contains("11") {
    print("favoriteGenres2 contains 11")
}

for genere in favoriteGenres2 {
    print(genere)
}

for gene in favoriteGenres2.sorted() {
    print(gene)
}

let oddDigits: Set = [1, 3, 5, 7, 9]
let evenDigits: Set = [0, 2, 4, 6, 8]
let singleDigitPrimeNumbers: Set = [2, 3, 5, 7]

oddDigits.union(evenDigits)

print(oddDigits.intersection(evenDigits))


oddDigits.subtracting(singleDigitPrimeNumbers)


let houseAnimals: Set = ["", ""]
let farmAnimals: Set = ["", "", "", "", ""]
let cityAnimals: Set = ["", ""]

houseAnimals.isSubset(of: farmAnimals)
farmAnimals.isSuperset(of: houseAnimals)
farmAnimals.isDisjoint(with: cityAnimals)


// 字典类型简化语法

var namesOfIntegers: [Int: String] = [:]

namesOfIntegers[16] = "sixteen"

var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]

print("has  \(airports.count) count")

airports["LHR"] = "London"

airports["LHR"] = "London Heathrow"



airports["LHR"] = "London Heathrow"

if let oldValue = airports.updateValue("Doubin AirPort", forKey: "DUB") {
    print("The old value for DUB was \(oldValue).")
}
    
if let airportName = airports["DUB"] {
    print("The name of the airport is \(airportName).")
} else {
    print("That airport is not in the airports dictionary.")
}


if let removedValue = airports.removeValue(forKey: "DUB") {
    print("The removed airport's name is \(removedValue)")
} else {
    print("he airports dictionary does not contain a value for DUB.")
}

for (cide, bame) in  airports {
    print("\(cide) \(bame)")
}

for name  in airports.keys {
    print(name)
}

let airportCodes = [String](airports.keys)

//  时间20220531


// 控制流

let  names2 = ["Anna", "Alex", "Brian", "Jack"]
for name in names2 {
    print("hello \(name)")
}


let numberOfLegs = ["spider":8, "ant":7 , "cat":9]
for (name,counts) in numberOfLegs  {
    print("\(name) have \(counts) legs")
}

for index in 1...5 {
    print("\(index) times 5 is \(5 * index)")
}

let base = 3
let power = 10
var answer = 2
for _ in 1...power {
    answer *= base
    print("\(base) to the power of \(power) is answer")
}

print("\(base) to the power of \(power) is answer")

let minutes = 10
for tickMark in 0..<minutes {
    print(tickMark)
}


let hours = 12
let hourInterval = 3
for tickMark in stride(from: 3, through: hours, by: hourInterval) {
    print("tickMark2 = \(tickMark)")
}

let finalSquare = 25
var board = [Int](repeating: 1, count: finalSquare + 1)


var square = 0
var diceRoll = 0

while  square < finalSquare {
    diceRoll += 1
    if diceRoll == 7 {
        diceRoll = 1
    }
    square += diceRoll
    if square < board.count {
        square += board[square]
    }
}

print("Game over")



let someCharacter: Character = "z"

switch someCharacter {
case "a":
    print("The first letter of the alphabet")
case "z":
    print("The last letter of the alphabet")
default:
    print("Some other character")
}


let anotherCharacter:  Character = "a"
switch anotherCharacter {
case "a","A":
    print("the letter A")
default:
    print("Not the letter A")
}


let approximateCount = 62
let countedThings = "moons orbiting Saturn"
let naturalCount: String
switch approximateCount{
case 0:
    naturalCount = "no"
case 1..<5:
    naturalCount = "a few"
case 5..<12:
    naturalCount = "dozens of"
default:
    naturalCount = "many"
}
print("there are \(naturalCount) count")

//元祖

let somePoint = (1,1)
switch somePoint {
case (0,0):
    print("\(somePoint) is at the origin")
case (_,0):
    print("\(somePoint) is at the X")
case (0,_):
    print("\(somePoint) is at the Y")
default:
    print("\(somePoint) is outside of the box")
}

let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0):
    print("on the x-axis with an x value of \(x)")
case (0, let y):
    print("on the y-axis with a y value of \(y)")
case let (x, y):
    print("somewhere else at (\(x), \(y))")
}

let yetAnotherPoint = (1, 2)
switch yetAnotherPoint {
case let (x,y) where x == y:
    print("\(x) ,\(y) is on the line x ==y")
case let (x,y) where x == -y:
    print("\(x) ,\(y) is on the line x == -y")
case let (x, y):
    print("\(x) ,\(y)  is just some arbitrary point")
}

let someCharacter2: Character = "e"
switch someCharacter2 {
case "a","e","i","o","u":
    print("\(someCharacter2) is a vowel")
case  "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
    "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
    print("\(someCharacter2) is a consonant")
default:
    print("\(someCharacter2) is not a vowel or a consonant")
}



let stillAnotherPoint = (9, 1)
switch stillAnotherPoint {
case (let distance,0) ,(0, let distance) :
    print("On an axis, \(distance) from the origin")
default:
    print("not on an axis")
}

let numberSymbol: Character = "o"  // 简体中文里的数字 3
var possibleIntegerValue: Int?
switch numberSymbol {
case "1", "١", "一", "๑":
    possibleIntegerValue = 1
case "2", "٢", "二", "๒":
    possibleIntegerValue = 2
case "3", "٣", "三", "๓":
    possibleIntegerValue = 3
case "4", "٤", "四", "๔":
    possibleIntegerValue = 4
default:
    break
}

if let integerValue = possibleIntegerValue {
    print("The integer value of \(numberSymbol) is \(integerValue).")
} else {
    print("An integer value could not be found for \(numberSymbol).")
}


let integerToDescribe = 1
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
    description += "a prime number, and also"
default:
    description += "an integer"
}
print(description)


let finalSquare2 = 25
var board1 = [Int](repeating: 0, count: finalSquare2 + 1)
board1[03] = +08; board1[06] = +11; board1[09] = +09; board1[10] = +02
board1[14] = -10; board1[19] = -11; board1[22] = -02; board1[24] = -08
var square1  = 0
var diceRoll1 = 0
square = 3
gameLoop: while square != finalSquare {
    diceRoll += 1
    if diceRoll == 7 { diceRoll = 1 }
    switch square + diceRoll {
    case finalSquare:
        // 骰子数刚好使玩家移动到最终的方格里,游戏结束。
        break gameLoop
    case let newSquare where newSquare > finalSquare:
        // 骰子数将会使玩家的移动超出最后的方格,那么这种移动是不合法的,玩家需要重新掷骰子
        continue gameLoop
    default:
        // 合法移动,做正常的处理
        square += diceRoll
        square += board[square]
    }
}
print("Game over!")


func greet(person:[String:String]) {
    guard let name = person["name"] else {
        return
    }
    print("hello \(name)")
    
    guard let location = person["location"] else {
        print("I hope the weather is nice near you.")
        return
    }
    
    print("I hope the weather is nice in \(location).")
}



greet(person: ["name":"john"])


if #available(iOS 10, macOS 10.12, *) {
    
}else {
    
}


func greet(person: String) -> String {
    let greeting = "hello ," + person + "!"
    return greeting
}

print(greet(person: "Annna"))

print(greet(person: "Brian"))

func greatAgain(person: String) -> String {
    return "hello again ," + person + "!"
}

print(greatAgain(person: "Anna"))



func sayHellowWord() -> String {
    return "hello world"
}

print(sayHellowWord())


func great(person:String, alreadyGreated:Bool) -> String {
    if alreadyGreated {
        return greatAgain(person: person)
    } else {
        return greet(person: person)
    }
}

print(great(person: "Tim", alreadyGreated: false))


func greetNew(person: String) {
    print("hello \(person)")
}
greetNew(person: "test1")

func printAndCount(string1: String) -> Int {
    print(string1)
    return string1.count
}

func printWithOutCounting(string: String) {
    let _ = printAndCount(string1: string)
}

printAndCount(string1: "helloworld")

printWithOutCounting(string: "helloword")


func mixmax(array:[Int]) -> (min: Int, max:Int)? {
    if array.isEmpty {
        return nil
    }
    var currentMin = array[0]
    var currentMax = array[0]
    for value in array[1..<array.count]  {
        if value < currentMin {
            currentMin = value
        } else if value > currentMax {
            currentMax = value
        }
    }
    return (currentMin, currentMax)
}

let bounds = mixmax(array: [1,3, 5, 7,9,19])

if let bounds = mixmax(array: [1,3, 5, 7,9,19]) {
    print("min is \(bounds.min) and max is \(bounds.max)")
}


func greetings(for person: String) -> String {
    "hello ," + person + "!"
}

print(greetings(for: "Dave"))

func anotherGreeting(for person: String) -> String {
    return "hello ," + person + "!"
}

print(anotherGreeting(for: "dave"))


func someFunction(firstParameterName: Int ,secondParameterName:Int) {
    
}

someFunction(firstParameterName: 1, secondParameterName: 2)

func someFunction(argumentLabel parameterName:Int) {
    
}

func greet(person: String ,from hometown: String) -> String {
    return "Hello \(person)!  Glad you could visit from \(hometown)."
}

print(greet(person: "Bill", from: "Cupertino"))


func someFuntion22(_ firstParameterName:Int, secondParameterName:Int) {
    
}
someFuntion22(1, secondParameterName: 2)

func somefuntionValue(parameterWithoutDefault: Int, parameterWithDefault:Int = 12) {
    
}

somefuntionValue(parameterWithoutDefault: 1, parameterWithDefault: 2)

somefuntionValue(parameterWithoutDefault: 4)

// 可变参数

func arithmeticMean(_ numbers: Double...) -> Double {
    var total:Double = 0
    for number in numbers {
        total += number
    }
    return total / Double(numbers.count)
}

arithmeticMean(1,2,3,4,10)

arithmeticMean(3, 8.25, 18.75)


// 输入输出参数

func swapTwoInts(_ a: inout Int, _ b: inout Int) {
    let temporaryA = a
    a  = b
    b  = temporaryA
}

var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, &anotherInt)//都加了 & 的前缀因为这些量是不能被修改的。当传入的参数作为输入输出参数时,需要在参数名前加 & 符,表示这个值可以被函数修改。

print("someInt is \(someInt) anotherInt is \(anotherInt)")


//函数类型

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

func multiplyTwoInts(_ a: Int, _ b: Int) -> Int {
    return a * b
}

func printHelloWorld() {
    print("hello word")
}


var mathFunction: (Int, Int) -> Int = multiplyTwoInts

print("Result: \(mathFunction(2,3))")

// 函数类型作为参数类型
func printMathResult(_ mathFunction: (Int , Int) -> Int, _ a: Int, _ b: Int) {
    print("Result:\(mathFunction(a,b))")
}
          
printMathResult(addTwoInts,  3,  5)

//函数类型作为返回类型
func stepForward(_ input: Int) -> Int {
    return input + 1
}

func stepbackWard(_ input: Int) -> Int {
    return input - 1
}

func chooseStepFunction(backward: Bool) -> (Int) -> Int {
    return backward ? stepbackWard : stepForward
}


var currentValue = 3
let moveNearerZero = chooseStepFunction(backward: true)


print("Counting to zero")
while currentValue != 0 {
    print("\(currentValue)")
    currentValue = moveNearerZero(currentValue)
}

print("zero!")


// 嵌套函数

func chooseStepFunction2(backward: Bool) -> (Int) -> Int {
    func stepForward(input:Int) -> Int {return input + 1}
    func stepBaclForward(input: Int) -> Int {return input - 1}
    return backward ? stepForward : stepForward
}

var currentValue2 = -4
let moveNearerToZero = chooseStepFunction2(backward: currentValue > 0)
if currentValue != 0 {
    print("\(currentValue)")
    currentValue =  moveNearerZero(currentValue)
}
print("zero")

// 闭包

let names23 = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
func backWard23 (_ s1: String , _ s2: String) -> Bool {
    return s1 > s2
}
 
var reversedNames = names23.sorted(by: backWard23)

reversedNames = names23.sorted(by: { (s1: String, s2: String) -> Bool in   return s1 < s2
})

var reversedNames2 = names23.sorted(by:{ s1, s2 in return s1 > s2})

var reversedNames23 = names23.sorted(by:{ s1, s2 in  s1 > s2})

var reversedNames233 = names23.sorted(by:{ $0 > $1})
print("reversedNames233=\(reversedNames233)")

var reversedNames2333 = names23.sorted(by: > )
print("reversedNames2333=\(reversedNames233)")


// 尾随闭包
func someFunctionThatTakesAClosure(closure: () -> Void) {
    // 函数体部分
}

someFunctionThatTakesAClosure ( closure: {
     // 闭包主题部分
  }
)

someFunctionThatTakesAClosure() {
    // 闭包主体部分
}

var reversedNames23334 = names23.sorted{ $0 > $1 }

let digitNames = [
    0: "Zero", 1: "One", 2: "Two",   3: "Three", 4: "Four",
    5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine"
]
let numbers = [16, 58, 510]


// 尾随闭包
let strings = numbers.map {
    (number) -> String in
    var number = number
    var output = ""
    repeat {
        output = digitNames[number%10]! + output
        number /= 10 // 1
    }  while number > 0
    return output
}
print(strings)


// 值捕获
func makeIncrementer(forIncrement amout: Int) -> ()-> Int {
    var runingTotal  = 0
    func incrementer() -> Int {
        runingTotal += amout
        print("runingTotal = \(runingTotal)")
        return runingTotal
    }
    return incrementer
}


let incrementByTen1 = makeIncrementer(forIncrement: 10)
incrementByTen1()
incrementByTen1()

let incrementBySeven = makeIncrementer(forIncrement: 7)
incrementBySeven()

//20220601 10:30
//逃逸闭包
//当一个闭包作为参数传到一个函数中,但是这个闭包在函数返回之后才被执行,我们称该闭包从函数中逃逸。当你定义接受闭包作为参数的函数时,你可以在参数名之前标注 @escaping,用来指明这个闭包是允许“逃逸”出这个函数的。
//一种能使闭包“逃逸”出函数的方法是,将这个闭包保存在一个函数外部定义的变量中。举个例子,很多启动异步操作的函数接受一个闭包参数作为 completion handler。这类函数会在异步操作开始之后立刻返回,但是闭包直到异步操作结束后才会被调用。在这种情况下,闭包需要“逃逸”出函数,因为闭包需要在函数返回之后被调用。例如:
var completionHanders: [() -> Void] = []
func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void )
{
    completionHanders.append(completionHandler)
}
//
//将一个闭包标记为 @escaping 意味着你必须在闭包中显式地引用 self。比如说,在下面的代码中,传递到 someFunctionWithEscapingClosure(_:) 中的闭包是一个逃逸闭包,这意味着它需要显式地引用 self。相对的,传递到 someFunctionWithNonescapingClosure(_:) 中的闭包是一个非逃逸闭包,这意味着它可以隐式引用 self。


func someFunctionWithNonescapingClosure(closure: () -> Void) {
    closure()
}

class SomeClass {
    var x = 10
    func doSomeThing() {
        someFunctionWithEscapingClosure {
            self.x = 100 //将一个闭包标记为 @escaping 意味着你必须在闭包中显式地引用 self
        }
        
        someFunctionWithNonescapingClosure {
            x = 200
        }
    }
}


let instance = SomeClass()
instance.doSomeThing()
print(instance.x) // 非逃逸闭包

completionHanders.first?()
print(instance.x) // 逃逸闭包 . 被调用才生效


//自动闭包
/*
自动闭包
自动闭包是一种自动创建的闭包,用于包装传递给函数作为参数的表达式。这种闭包不接受任何参数,当它被调用的时候,会返回被包装在其中的表达式的值。这种便利语法让你能够省略闭包的花括号,用一个普通的表达式来代替显式的闭包。
我们经常会调用采用自动闭包的函数,但是很少去实现这样的函数。举个例子来说,assert(condition:message:file:line:) 函数接受自动闭包作为它的 condition 参数和 message 参数;它的 condition 参数仅会在 debug 模式下被求值,它的 message 参数仅当 condition 参数为 false 时被计算求值。
自动闭包让你能够延迟求值,因为直到你调用这个闭包,代码段才会被执行。延迟求值对于那些有副作用(Side Effect)和高计算成本的代码来说是很有益处的,因为它使得你能控制代码的执行时机。下面的代码展示了闭包如何延时求值。
 */

var customersInLine = ["Chris", "Alex", "Ewa", "Barry", "Daniella", "Daniella2", "Daniella3", "Daniella4"]
print("customersInLine=\(customersInLine.count))")

let customerProvider = {customersInLine.remove(at: 0)}
print(customersInLine.count)

print("Now serving \(customerProvider())!")
print(customersInLine.count)


func serve(customer customerProvider: () -> String) {
    print("Now serving \(customerProvider())!")
}

serve(customer: {customersInLine.remove(at: 0)})
func serve(customer customerProvider: @autoclosure () -> String) {
    print("Now serving \(customerProvider())!")
}

print(serve(customer: customersInLine.remove(at: 0)))

//如果你想让一个自动闭包可以“逃逸”,则应该同时使用 @autoclosure 和 @escaping 属性。@escaping 属性的讲解见上面的 逃逸闭包。

var customerProviders: [() -> String] = []

func collectCustomerProviders(_ customerProvider:@autoclosure @escaping () -> String) {
    customerProviders.append(customerProvider)
}
collectCustomerProviders(customersInLine.remove(at: 0))
collectCustomerProviders(customersInLine.remove(at: 0))

print("Collected \(customerProviders.count) closures.")

for customerProvider in customerProviders {
    print("Now serving \(customerProvider())")
}

相关文章

  • rust 闭包与同步

    rust 闭包与同步 rust 闭包 rust闭包中主要包括两个部分,闭包参数和闭包环境变量。闭包函数和函数参数使...

  • 第一部分: 基础部分 ~ 闭包函数

  • 闭包

    闭包 闭包和函数的区别:闭包可以储存一部分变量,函数不能,函数传什么有什么值 函数与匿名函数的区别,匿名函数可以完...

  • swift 基础(二)闭包、对象和类、setter&ge

    //闭包 //闭包的函数体部分由关键字 in 引入。 该关键字表示闭包的参数和返回值类型定义已经完成,闭包函数体即...

  • 闭包&定时器-Assignment

    1.什么是闭包? 有什么作用 闭包就是能够读取其他函数内部变量的函数!!!闭包由两部分组成——函数以及创建该函数的...

  • 什么是闭包?

    一、闭包的定义 1、闭包的构成首先,闭包由两部分构成:函数、创建该函数的环境,环境由闭包创建时在作用域中的任何局部...

  • 闭包

    闭包的定义 闭包有两个部分组成,一个函数,一个该函数外部作用域(词法环境)的引用。闭包实现内部函数有权访问外部函数...

  • Swift3.0 基础语法2

    主要分为以下几个部分: 1.函数1.1 函数的定义1.2 闭包1.3 多线程GCD的变化1.4 闭包循环引用1.5...

  • 对于闭包的一些理解

    闭包是由函数和与其相关的引用环境组合而成的实体(wikipedia-闭包) 闭包由两部分组成:一是函数,二是与这个...

  • 闭包的使用javascript:void(null)

    闭包是什么 个人理解,闭包是就是函数中的函数,里面的函数可以访问外面函数的变量,外面的变量的是这个内部函数的一部分...

网友评论

      本文标题:第一部分: 基础部分 ~ 闭包函数

      本文链接:https://www.haomeiwen.com/subject/ftlxmrtx.html