Can't reference two variables at once in SwiftUI

Viewed 79

This is really confusing me. When I do something to one variable, it's fine. But when one variable operates on another, it won't compile.

var body: some View {

  GeometryReader { geo in
    
    let mUnit = (geo.size.width / 7)
    
    let mPadding = mUnit / 10
    
    let NotWorking = mUnit - mPadding
1 Answers

works without any problems for me on macos 12.beta, xcode 13.beta, target ios 15 and macCatalyst. This is the code I used for testing:

import SwiftUI

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

struct ContentView: View {

    var body: some View {
        GeometryReader { geo in
            let mUnit = (geo.size.width / 7)
            let mPadding = mUnit / 10
            let NotWorking = mUnit - mPadding
            let _ = print("---> NotWorking: \(NotWorking)")
            Text("\(NotWorking)")
        }
    }
}

What system are you on?

If the compiler is confused about the types, you could try this:

        let mUnit: CGFloat = geo.size.width / CGFloat(7)
        let mPadding: CGFloat = mUnit / CGFloat(10)
        let NotWorking: CGFloat = mUnit - mPadding
Related