Post

Replies

Boosts

Views

Activity

Reply to Binding with optional Value causes runtime crash
The property is already a published entity in an observable object. Any changes to the name property will be propagated to the SwiftUI view. The issue was your nameBinding property. Unwrapping viewModel.name is you really had to do then passed the value into a Binding init constant as seen here ChildView(selectedName: .constant(name)). struct ParentView: View { @StateObject var viewModel = Model() var body: some View { VStack(alignment: .leading, spacing: 8) { Text("Name is \(viewModel.name ?? "nil")") Button("Make name nil") { viewModel.makeNameNil() } if let name = viewModel.name { ChildView(selectedName: .constant(name)) } } .padding() } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’23
Reply to Using swift or other tool to tell if printer is connected and ready for prints
This will only be true or false if a printer is defined and available and the NSPrinter.printerNames list is non-empty. func isConnectedToPrinter() -> Bool { let printers = NSPrinter.printerNames return !printers.isEmpty } see this GitHub resource on how to query the info you're seeking. https://github.com/marc-medley/004.42Apple_CorePrintingExample/blob/3481a1c8c253f98bbff19741a13cd2c9b8d1d88b/PDFPagePrinter/PDFPagePrinter/PrintData.swift#LL190C9-L203C10 // Printer State var printerState: PMPrinterState = 0 let _ = PMPrinterGetState(pmPrinter, &printerState) d["printerState"] = printerState switch printerState { case UInt16(kPMPrinterIdle) : d["printerStateName"] = "kPMPrinterIdle" case UInt16(kPMPrinterProcessing) : d["printerStateName"] = "kPMPrinterProcessing" case UInt16(kPMPrinterStopped) : d["printerStateName"] = "kPMPrinterStopped" default: d["printerStateName"] = "UNSPECIFIED" } Sean
Topic: UI Frameworks SubTopic: AppKit Tags:
May ’23