在Apple SwiftUI Tutorial: Drawing Paths and Shapes上,显示以下语法:
struct Badge: View {
var body: some View {
GeometryReader { geometry in
Path { path in
...
}
.fill(Color.black)
}
}
}
我没有在Swift docs on structs中看到语法,并且感到非常困惑。
另外,怎么可能知道
geometry
在GeometryReader
语句中,无论意味着什么?
最佳答案
这是尾随闭包的简化语法,因此
Path { path in
...
}
与...相同
Path( { (path) in
...
})
的
Path
构造函数/// Initializes to an empty path then calls `callback` to add
/// the initial elements.
public init(_ callback: (inout Path) -> ())
顺便说一句,此语法已在SwiftUI中广泛使用,因此您会在
VStack { .. }
,GeometryReader { g in ... }
,ScrollView { ... }
和SwfitUI框架的几乎所有其他UI元素中看到相同的语法。
关于ios - 使用{…in}的Swift闭包语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62341839/