ios - 为什么闭包的实际主体永远不会被执行?
问题描述
来自 Apple 的 Swift 编程语言(Swift 5.2 Beta)
func someFunctionThatTakesAClosure(closure: () -> Void) {
// function body goes here
print("Declaring Function")
}
// Here's how you call this function without using a trailing closure:
someFunctionThatTakesAClosure(closure: {
// closure's body goes here
print("Without Trailing Closure Syntax")
})
// Here's how you call this function with a trailing closure instead:
someFunctionThatTakesAClosure() {
// trailing closure's body goes here
print("Trailing Closure Syntax")
}
摘自:Apple Inc. “The Swift Programming Language (Swift 5.2 beta)”。苹果图书。
在这些示例中,它在someFunctionThatTakesAClosure
被调用两次时只打印两次声明函数,我想知道这两个闭包的主体是如何没有被调用的?
解决方案
您需要更改为以下内容:
func someFunctionThatTakesAClosure(closure: () -> Void) {
print("Declaring Function")
closure()
}
推荐阅读
- javascript - Threejs项目中移动和桌面浏览器之间画布的document.createElement不一致
- python - TypeError: dtype 对象的图像数据不能转换为浮点数
- reactjs - Styled-Components:具有多个道具/状态的条件样式
- android - 在 Android 问题中插入动态视图
- javascript - 如何获取鼠标单击的位置并在图像上添加标记(标记)?
- azure - 使用 Azure CNI 和站点到站点 VPN VNet 时如何从 pod 子网中拆分 AKS 群集子网
- kubernetes - 将 Kubernetes 中的各个点联系起来的良好做法是什么?
- amazon-web-services - 如何防止 Terraform 模块创建重复的 API 网关资源?
- javascript - 如何在一个导入的对象中组合 CSS 类
- javascript - 每个 React 类方法的“函数缺少返回类型”