SwiftUI Stepper crashes no matter how I try to use it

XCode 11b5 macOS 10.15

This slider works for me:

Slider(value: $rect.recSize, in: 2.0...200.0, step: 2.0)

But when I try a stepper, launch my app, mouse over the stepper up/down arrows, the app crashes. Doing this:

            Stepper(onIncrement: {
                self.rect.recSize += 1
            }, onDecrement: {
                self.rect.recSize -= 1
            }, label: { Text("size: \(rect.recSize)")}
            )

Full context:

MacOS app

ContentView.swift

//
//  ContentView.swift
//  test-0001
//

import SwiftUI
import Combine

class Shape: ObservableObject {
    @Published var recSize:Float = 50.0
}

struct Badge: View {
    @State var counter = 0
    @State private var name: String = "Hello"
//    @EnvironmentObject var rect: Shape
    @ObservedObject var rect = Shape()

    var body: some View {
        VStack {
            HStack {
                TextField("RecSize", text: $name)
//                Slider(value: $rect.recSize, in: 2.0...200.0, step: 2.0)
                Stepper("Size", value: $rect.recSize, in: 2.0...200.0, step: 2.0)

//                Stepper(value: $rect.recSize, in: 2.0...200.0, step: 2.0, label: {Text("size")})
                
//                Stepper(onIncrement: {
//                    self.rect.recSize += 1
//                }, onDecrement: {
//                    self.rect.recSize -= 1
//                }, label: { Text("size: \(rect.recSize)")}
//                )

            }
                            
            Rectangle()
            
                .stroke(Color.blue, lineWidth: 1)
                .frame(width: (CGFloat(rect.recSize)), height: CGFloat(rect.recSize))


            Text("Slider: \(rect.recSize)")
            Text("Name: \(name)")
        }
        .foregroundColor(Color.black)
        .font(.title)
            
    }
}

#if DEBUG
//struct ContentView_Previews: PreviewProvider {
//    static var previews: some View {
//        ContentView()
//    }
//}
//
//let rect = Shape()
struct Badge_Previews: PreviewProvider {
    static var previews: some View {
        Badge() //.environmentObject(rect)
    }
}

#endif

Document.swift

//
//  Document.swift
//  test-0001
//

import Cocoa
import SwiftUI

class Document: NSPersistentDocument {

    override init() {
        super.init()
        // Add your subclass-specific initialization here.
    }

    override class var autosavesInPlace: Bool {
        return true
    }

    override func makeWindowControllers() {
        // Use NSHostingView for window content view.
//        var rect = Shape()
        let window = NSWindow(
            contentRect: NSRect(x: 0, y: 0, width: 240, height: 300),
            styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
            backing: .buffered, defer: false)
        
        window.center()

        window.contentView = NSHostingView(rootView: Badge()) //.environmentObject(rect))

        let windowController = NSWindowController(window: window)
        self.addWindowController(windowController)
    }

}

Am I in the wrong place to ask a question like this?

Probably better to ask on the Apple developer forums. This site is about the Swift language and infrastructure. Your question in about Apple frameworks.

See Apple developer forums: https://forums.developer.apple.com/thread/120753

Seems to be fixed now with Xcode Beta 7 and 10.5 Beta 7.

Terms of Service

Privacy Policy

Cookie Policy