美文网首页SwiftUISwiftUI
SwiftUI 里的 swift 闭包总结

SwiftUI 里的 swift 闭包总结

作者: 小牛仔kx | 来源:发表于2019-08-26 15:49 被阅读0次

创建 UI 时的闭包使用

在 SwiftUI 里闭包出现的频率特别高,这里我重新梳理了下闭包的定义。

关于闭包

闭包表达式语法的一般形式如下:

{(parameters) -> return type in
  // 代码
}

闭包表达式写在花括号({})里,关键字in用来分隔闭包的参数、返回值与闭包体内的语句

// 传递闭包个 sort 方法
arr.sort(by: { (a: Int, b: Int) -> Bool in
    return a > b
})

// 闭包可以利用 Swift 的类型推断能力,可以省略返回类型,以及参数的类型
arr.sort(by: { a, b in
    return a > b
})

// 这里移除了关键字return。不是所有的闭包语句都可以移除 return 关键字
// 这里可以是 因为只有一个表达式 (i < j)。如果存在更多表达式,那么显式的 return 就是必需的。
arr.sort(by: { a, b in a > b })

// Swift 提供了快捷参数名,可以在内联闭包 表达式中使用
arr.sort(by: { $0 > $1})

// 如果一个闭包是以一个函数的最后一个参数传递的,那么它就可以在函数的圆括号以外内联。
arr.sort { $0 > $1 }

闭包赋值变量

// 普通方式
var str: String = "str"

// 闭包运行赋值
var str2: String = {
    return "str2"
}()

// 基于闭包原理简化
var str3: String = {
    "str3"
}()

// 如果不需要传递实参,闭包体前的"="号,和末尾的"()"也可以省略
var str4: String {
    "str4"
}

SwiftUI 里的闭包

在声明式的 UI 创建里大量使用闭包,比如

import SwiftUI

struct Text: View {
    var body: some View {
        Button(action: {
            print("Button Click")
        }) {
            Text("Hello World!")
        }
    }
}

这里创建 View、Button 都使用了闭包,看了下 Button 的实现,如下

public struct Button<Label> : View where Label : View {

    /// Creates an instance for triggering `action`.
    ///
    /// - Parameters:
    ///     - action: The action to perform when `self` is triggered.
    ///     - label: A view that describes the effect of calling `action`.
    public init(action: @escaping () -> Void, @ViewBuilder label: () -> Label)

    /// Declares the content and behavior of this view.
    public var body: some View { get }

    /// The type of view representing the body of this view.
    ///
    /// When you create a custom view, Swift infers this type from your
    /// implementation of the required `body` property.
    public typealias Body = some View
}

在 init 方法里设置了 2 个参数,都是函数类型,在使用时可以利用 swift 的 尾部闭包语法 :如果一个闭包是以一个函数的最后一个参数传递的,那么它就可以在函数的圆括号以外内联,所以 Button 可以如此写

Button(action: {
  print("Button Click")
}) {
  Text("Hello World!")
}

也可以这样写

Button(action: { print("Button Click") }, label: { Text("Hello World!") })

相关文章

  • SwiftUI 里的 swift 闭包总结

    创建 UI 时的闭包使用 在 SwiftUI 里闭包出现的频率特别高,这里我重新梳理了下闭包的定义。 关于闭包 闭...

  • Swift学习:闭包

    本篇将详细总结介绍Swift闭包的用法;闭包是自包含的函数代码块,可以在代码中被传递和使用。Swift中的闭包与C...

  • Swift-闭包

    Swift 闭包 函数 ()->() Swift 中的闭包和 Objective-C 中的 block 类似,闭包...

  • Swift闭包和函数

    函数在Swift中只是一种特殊的闭包,闭包在Swift语言中是一等公民,支持闭包嵌套和闭包传递。Swift中的闭包...

  • swift4 闭包

    swift 闭包 闭包:swift 中 函数是闭包的一种类似于oc的闭包闭包表达式(匿名函数) -- 能够捕获上下...

  • iOS&Swift&OC 闭包和Block的相互转化

    一、Swift的闭包 -> OC的block 二、OC的block -> Swift的闭包

  • Swift中的闭包

    在Swift中有两种闭包,逃逸闭包(@escaping)和非逃逸闭包(@nonescaping)。从Swift 3...

  • 100 Days of Swift - Day 06 - 闭包(

    100 Days of Swift - Day 06 - 闭包Closures 6.1 闭包 Swift函数也属于...

  • swift学习

    * 闭包 * 闭包作为属性 ```swift // 声明闭包类型 typealias callba...

  • iOS swift 逃逸闭包(@escaping)和非逃逸闭

    iOS swift 逃逸闭包(@escaping)和非逃逸闭包 (@noescaping) 逃逸闭包: 逃逸闭包...

网友评论

    本文标题:SwiftUI 里的 swift 闭包总结

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