Post

Replies

Boosts

Views

Activity

Still wondering about objectWillChange (vs objectDidChange) magic :)
Hi all, The WWDC video offers (at about 16:45) as short explanation as to why it's "objectWillChange" instead of "objectDidChange". He said it's because SwiftUI needs to coalesce the changes. Okay, but then how does it know when the changes have ended? It seems like you'd need two events. Something like: self.objectWillChange.send() self.foo = ... self.bar = ... self.objectHasFinishedChanging.send() or self.objectChanges { 		self.foo = ... 		self.bar = ... }
4
0
2.5k
Feb ’21
SKTestSession failTransactionsEnabled not working
If I purchase a product with a test session in my unit test, everything works as expected. I see a purchased transaction on my payment queue. But if I set the property to make it fail, nothing happens. Shouldn't I observe a transactions with transactionState == .failed?let session = try! SKTestSession(configurationFileNamed: "Configuration") session.disableDialogs = true session.clearTransactions() session.failTransactionsEnabled = true session.failureError = .unknown session.buyProduct(productIdentifier: productID) Has anyone used this? This observer normally gets called. But not when I set failTransactionsEnabled.    public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {     
1
0
832
Mar ’21
Button text is invisible after dark/light mode change
I'm working on a macOS SwiftUI app. I notice that Button text does not change color properly when the app changes between Dark and Light mode. Shouldn't the builtin Button be handling this automatically? The Text views seem to handle it fine and switch their text from white to black. The Button text can become completely invisible when I go from Dark to Light mode. Restarting the app fixes it. I can change the Dark/Light mode manually in System preferences, or it changes at certain times of day because I have it set, usually, to "Auto".
7
0
2.1k
Mar ’21
Differentiating between "return" and focus loss in TextField onCommit
I have a TextField in my toolbar that I use for a search function. I want to trigger the search by hitting the "return" key in this field. But if I do it in onCommit, the search also gets triggered when the user un-focuses the field. Is there a way to respond to just the "return" key? TextField("Search", text: $searchQuery) { editing in print("onEditingChanged \(editing)") } onCommit: { // Problem: this is triggered by both // 1. Return key // 2. Losing focus     searchModel.startSearch(query: searchQuery) }
7
0
2.4k
Mar ’21
SwiftUI bug: Alert shows twice?
I'm running this on macOS. I looks like a bug to me. If I activate the menu item and confirm the alert, the alert pops up again. It only double-shows once; then it behaves correctly. In my real app, it double-shows every time. If I uncomment that DispatchQueue.main.async, it "fixes" it. macOS 11.2.3, Xcode 12.4. swift @main struct DoubleAlertApp: App {     @State var showAlert: Bool = false     @StateObject var model: Model = .init()     var body: some Scene {         WindowGroup {             ContentView().environmentObject(model)                 .background(EmptyView()                     .alert(isPresented: $showAlert) {                          Alert(title: Text("Test"),                                message: Text("This will do something"),                                primaryButton: .cancel(),                                secondaryButton: .destructive(Text("Do it"), action: confirmedAction))                     })         }.commands {             CommandGroup(replacing: CommandGroupPlacement.newItem) {                 Button(action: testAlert) {                     Text("Test Alert")                 }.keyboardShortcut("t")             }         }     }     private func testAlert() {         showAlert = true     }     private func confirmedAction() {         print("confirmedAction")         // DispatchQueue.main.async {             self.model.change()         //}     } } class Model: ObservableObject {     init() {}     @Published var foo: Int = 0     func change() {         objectWillChange.send()         foo += 1     } } struct ContentView: View {     @EnvironmentObject var model: Model     var body: some View {         Text("Hello. \(model.foo)").padding()     } }
2
0
2.3k
Mar ’21
How to set cursor to show drag possibility - onHover fails
I have a View with a DragGesture to resize something, so I tried using onHover to set the macOS cursor to the right resize icon. It doesn't work 100%, because the drag will reset the cursor. Then I have to move the pointer out of the view and back in, to re-trigger the onHover and get the resize cursor again. swift private struct HeaderEdgeDragArea: View {     var drag: some Gesture {         DragGesture(minimumDistance: 0, coordinateSpace: .global)             .onChanged { value in ...             }             .onEnded { _ in ...             }     }     var body: some View {         Color.gray.zIndex(2.0).frame(width: 1.0)             .overlay(                 Rectangle().frame(width: 8)                     .foregroundColor(.clear)                     .contentShape(Rectangle())                     .border(Color.red, width: 0.5)                     .onHover { hovering in                         print("hovering: \(hovering)")                         if hovering {                             NSCursor.resizeLeftRight.push()                         } else {                             NSCursor.pop()                         }                     }                     .gesture(drag)             )     } }
0
0
510
Apr ’21
How to respond to builtin main menu items like "Select All" in SwiftUI?
I know how to add items to the main menu. But what if I want to connect a handler to one that is already there by default? (For example "Select All"). WindowGroup { ContentView() }.commands {     CommandGroup(after: CommandGroupPlacement.pasteboard) {   Button("Select All") { selectAll() } } That adds a second "Select All" menu item. If I use CommandGroup(replacing: ...) then it replaces others, not just the "Select All"
0
1
597
Apr ’21
Logger and signposts?
I've been using the os.Logger API, but it looks like I need to create an OSLog as well if I want to use the signpost API for tracking performance. Is that true, or is there some way get an underlying OSLog from a Logger? Then I could write something like: swift extension os.Logger { func signpost(...) { os_signpost(.begin, ... self.osLog, ...) } }
1
0
668
Apr ’21
Can't run unit tests with Xcode 12.5. Code signing?
After updating to 12.5, I now get an error trying to run my unit tests. This is for a macOS app. dyld: warning: could not load inserted library '/[...]/DerivedData/[...]/Contents/Frameworks/libXCTestBundleInject.dylib' into hardened process because no suitable image found.  Did find: /[...]/libXCTestBundleInject.dylib: code signature in (/[...]/libXCTestBundleInject.dylib) not valid for use in process using Library Validation: mapped file has no Team ID and is not a platform binary (signed with custom identity or adhoc?) Anyone else face and fix this, or understand what it is? I changed the "Signing Certificate" in my test target to "Sign to Run Locally" (from "Development") to see if that would help. It didn't fix it.
3
0
4.6k
Jun ’21
Upgrading app's UIDocument format from Data to FileWrapper?
Hi,I had a document-based iOS app working, but want to change it so it saves to a package. Seems like it's better when big chunks of a file may not be changing. In Xcode, under the Target > Info > Exported UTI > Conforms To, I had: "public.data, public.content". If I change that to "com.apple.package", then I can't open my old files to upgrade them. But if I *add* "com.apple.package", then the app opens both kinds as desired. I wonder if having it conform to all three of those types is going to cause other problems.Rob
Topic: UI Frameworks SubTopic: UIKit Tags:
3
0
1.5k
Jul ’21
Popping back multiple levels?
I have a UI where you can navigate/push views like this: Root view > List of things > View thing > Edit thingThe "Edit thing" view can also delete it. After a delete, I want it to pop back to the "List of things". Best I've got now is to call `presentationMode.wrappedValue.dismiss()` on the "Edit thing" view, and then again in the "View thing" view, but that time inside DispatchQueue.main.async { }. It works but the double animation is kind of clunky.Is there a better way?
3
0
7.5k
Sep ’21
Can't use protocols with SwiftUI models?
I've been using protocols to help model a hierarchy of different object types. As I try to convert my app to use SwiftUI, I'm finding that protocols don't work with the ObservableObject that you need for SwiftUI models. I wonder if there are some techniques to get around this, or if people are just giving up on "protocol oriented programming" when describing their SwftUI models? There is example code below. The main problem is that it seems impossible to have a View that with an model of protocol `P1` that conditionally shows a subview with more properties if that model also conforms to protocol `P2`.For example, I'm creating a drawing/painting app, so I have "Markers" which draw on the canvas. Markers have different properties like color, size, shape, ability to work with gradients. Modeling these properties with protocols seems ideal. You're not restricted with a single inheritance class hierarchy. But there is no way to test and down-cast the protocol...protocol Marker : ObservableObject { var name: String { get set } } protocol MarkerWithSize: Marker { var size: Float { get set } } class BasicMarker : MarkerWithSize { init() {} @Published var name: String = "test" @Published var size: Float = 1.0 } struct ContentView<MT: Marker>: View { @ObservedObject var marker: MT var body: some View { VStack { Text("Marker name: \(marker.name)") if marker is MarkerWithSize { // This next line fails // Error: Protocol type 'MarkerWithSize' cannot conform to 'MarkerWithSize' // because only concrete types can conform to protocols MarkerWithSizeSection(marker: marker as! MarkerWithSize) } } } } struct MarkerWithSizeSection<M: MarkerWithSize>: View { @ObservedObject var marker: M var body: some View { VStack { Text("Size: \(marker.size)") Slider(value: $marker.size, in: 1...50) } } }Thoughts?
3
1
9.4k
Sep ’21
Too many empty "required" UIView.init(coder:) methods
Hi,I have a lot of UIViews where the compiler forces me to add an init(coder:) initializer, like this:class FooView : UIView /* or a UIView subclass */ { ... required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } ... }It claims it's required but my program runs fine without it. I do not create the views from an archive.This makes me wonder if something is wrong here with the design of the library, or the concept of a 'required' initializer. What do people think? Does it make sense or is this a wart? If so, can it be fixed?Rob
Topic: UI Frameworks SubTopic: UIKit Tags:
5
1
2.8k
Sep ’21
Can't interpolate a Bool in SwiftUI Text?
Simple code like this gives an error. struct MyView: View {     @State private var test: Bool = false     var body: some View {           Text("Hello. \(test)") The error: Instance method 'appendInterpolation(_:formatter:)' requires that 'Bool' inherit from 'NSObject' What is going on?
2
1
3.9k
Sep ’21