I'm brand new to Swift and unable to wrap my head around a code snippet that uses Optional binding. Here is the code:
struct AWorkingView: View {
var frameSize: CGSize?
init(frameSize: CGSize? = nil) {
self.frameSize = frameSize
}
var body: some View {
GeometryReader { geometry in
if let frameSize, let frameRect = getframeRect(geometrySize: geometry.size, frameSize: frameSize){
ZStack {
VStack {
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
} else {
}
}
}
func getframeRect(geometrySize: CGSize, frameSize: CGSize) -> CGRect {
CGRect(x: 10, y: 10, width: 10, height: 10)
}
}
The above throws an error: Initializer for conditional binding must have Optional type, not 'CGRect'
for that let frameRect = ...
conditional binding.
But works when I do this:
...
var body: some View {
GeometryReader { geometry in
if let frameSize {
let frameRect = getframeRect(geometrySize: geometry.size, frameSize: frameSize)
ZStack {
...
}
Why is frameRect
expected to be CGRect?
and not just CGRect
in the conditional binding?
Thanks in advance!