Post

Replies

Boosts

Views

Activity

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
SwiftUI crash: AttributeGraph precondition failure
My app crashes and Xcode shows no stack trace. It just pops up some line of assembly language in __pthread_kill, and shows this in the console: [error] precondition failure: invalid value type for attribute: 230424 (saw PreferenceKeys, expected ViewList) AttributeGraph precondition failure: invalid value type for attribute: 230424 (saw PreferenceKeys, expected ViewList). Seems like a bug in SwiftUI. Any ideas? This is on macOS 11.
7
1
9.1k
Mar ’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
822
Dec ’20
SwiftUI vs. Autolayout - anyone else missing constraints?
There are some useful things that I could do with Autolayout that still seem near impossible with SwiftUI. For some of these trickier situations, I've seen example code (not from Apple) that uses SwiftUI's "preferences" plus GeometryReader to achieve something like a constraint. But the code is very ugly (in my opinion), and it sometimes logs runtime warning messages about changing state during layout. And I've never seen anyone from Apple say "yes, that's how we imagined you using that preferences API". So it seems like a hack. Examples of things that are easy in Autolayout but have no obvious equivalent in SwiftUI, that I know of: Aligning elements in different branches of view tree For example, a form-like layout with labels and editable fields. You want to say "make the labels equal width, or trailing-aligned". Each row might be in a HStack. If you make each column a VStack, you have the same problem in the vertical direction, if some of your right-side fields are different heights.					 Name: [ text field ] Favorite color: [ text field ] 		Description: [ text area ] 								 [					 ] 				Enabled: [checkbox] This is the kind of thing I've seen hacked with "preferences". 2. Center a label but slide it if needed | Left text			 Center text							 | Maybe that's a header at the top of the screen. Imagine for some some languages or font preference, the left side text grows. You'd want to slide the center text over. That's easy with autolayout and constraint priorities. I have no clue how to do it with SwiftUI.| Long Russian left text		Center text			 | Those are two examples I can remember. Maybe people can post more. I'd love to know what Apple's SwiftUI devs think of that Preferences quasi-constraint hack, or if people have nice solutions to the examples above.
0
0
924
Oct ’20
Bad: OutlineGroup is not lazy loading children?
This appears to severely limit the usefulness of hierarchical lists in SwiftUI. I want to use the new hierarchical list/outline to display a filesystem tree. For data to pass to OutlineGroup, I created a class named FileSystemNode, and gave it a computed children property. When the getter is first called, it will read its directory contents to return a list (if it is a directory). Problem is, when the OutlineGroup is first displayed, even though it is collapsed on-screen to a single node, it calls children and recurses over the entire filesystem. Is there a way to stop this? If not, I hope it gets fixed before release. (This is on macOS Big Sur beta)
3
3
1.9k
Oct ’20
How to build a replacement for sandbox-exec?
Hi, I want something like sandbox-exec, so I can run things that I don't trust, and restrict their ability to read or write files to only certain locations. Like most software devs I have to download and run lots of code from the internet and the danger of this really annoys me. Unfortunately sandbox-exec is marked as deprecated and the APIs in sandbox.h say "No longer supported". I notice there is some new stuff in the Apple docs about "hypervisors" and "virtualization". https://developer.apple.com/documentation/hypervisor https://developer.apple.com/documentation/virtualization Would these APIs allow me to start and control a virtual copy of my macOS, to serve like a sandbox? Are there other solutions that people use? As an example, say that I need to download and run a copy of memcached. It's a typical open source project – you unpack a source tgz, then run configure; make and get a binary. Now I want to run that without worrying that some hacker injected a piece of evil code to copy my files and send them somewhere. So I want to say "run this binary, while disallowing file reads and writes, except for directories X,Y,Z, and disallowing network connections, except for listening on port 1234."
12
0
4.7k
Sep ’20
Hierarchical List / OutlineGroup - can't change data?
Does anyone use the new hierarchical data List (ie, OutlineGroup) with a model where the tree data changes? I'm on Big Sur beta. I have code like this: List([treeRoot], children: \.children) { item in ... Say the treeRoot (observed object) gets a new child. The view tries to re-render and fails: 2020-09-21 ... [General] NSOutlineView error inserting child indexes <_NSCachedIndexSet: 0x600000760000>[number of indexes: 1 (in 1 ranges), indexes: (3)] in parent 0x0 (which has 1 children).
1
0
785
Sep ’20
Why do some keybindings not have the delete (-) button to remove?
I'm trying to edit my keybindings in Xcode's preferences. Sometimes when I click a binding, I get the "-" button to remove it, but other times I don't. For example, in Xcode 12 now, the shortcut for "Analyze (Product Menu)" has no way to remove it. I rarely use this and I don't want it taking up a binding. (I also wish Xcode would allow multi-stroke bindings like Emacs and IntelliJ. I can make more mnemonic bindings that way. But that's another story.)
0
0
294
Sep ’20
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 { &#9;&#9;self.foo = ... &#9;&#9;self.bar = ... }
4
0
2.5k
Jun ’20
Setting @State AnyCancellable? to nil doesn't deallocate/cancel?
Does @State keep hidden copies of it's previous values somewhere? I have a button that starts an async process. The button handler subscribes to a Combine publisher and saves the AnyCancellable in a @State var. Later, a cancel button sets the state var to nil. @State private var mySubscription: AnyCancellable? = nil When I set that to nil again, the subscription is not deallocated. If I make the simple change of moving mySubscription to a global var, or a property of an observed object, then it works as expected - the subscription is cancelled and deallocated. (This is SwiftUI 2019, Xcode 11, not the new beta stuff.)
1
0
995
Jun ’20
Popping back multiple levels?
I have a UI where you can navigate/push views like this: Root view &gt; List of things &gt; View thing &gt; 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
Jun ’20
Images in documentation comments not working?
Does anyone use this? I can't get it working. I'm talking about code in normal swift files, not playgrounds.Example:/// ![testdiagram](/Users/me/fullpathto/test-image.png) /// ![test xcode image](http://devimages.apple.com.edgekey.net/assets/elements/icons/128x128/xcode.png) ///Neither one displays anything in the documentation popover or the Quick Help Inspector.The docs act like it works: https://developer.apple.com/library/archive/documentation/Xcode/Reference/xcode_markup_formatting_ref/Images.html#//apple_ref/doc/uid/TP40016497-CH17-SW1A lot of my code would really benefit from diagrams in documentation. I'd love to be able to use this.
3
0
1.8k
Mar ’20
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&lt;MT: Marker&gt;: 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&lt;M: MarkerWithSize&gt;: 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.3k
Feb ’20
How does SwiftUI update if objectWillChange fires *before* change
I'm wondering how SwiftUI updates work in connection with ObservableObjects. If a SwiftUI View depends on an `ObservableObject`, the object's `objectWillChange` publisher fires, and SwiftUI learns about the change, before the change happens. At this point, SwiftUI can't re-render a View, right? Because the new properties aren't there yet. So what does SwiftUI do? Does it schedule the change for later? That doesn't make sense either - how can it know when the object will be ready to be used in a new rendering of the UI? ~ Rob
3
0
5.3k
Jan ’20
Simple layout still fails
I'm surprised this simple code still doesn't work on iOS 13.3 / Xcode 11.3. On my iPhone SE it's almost all off screen.It's just two pieces of text, side by side, and two pickers, side by side. Anyone know a workaround? struct ContentView: View { @State var choice: Int = 10 @State var choice2: Int = 10 var body: some View { return VStack { HStack { Text("Some text here") Spacer() Text("Foo baz") } HStack { Picker(selection: self.$choice, label: Text("C1")) { ForEach(0..&lt;10) { n in Text("\(n) a").tag(n) } } Picker(selection: self.$choice2, label: Text("C2")) { ForEach(0..&lt;10) { n in Text("\(n) b").tag(n) } } } } } }
4
1
3.9k
Dec ’19