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?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
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)
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.
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
Hi,I'm trying to port this to Swift. https://developer.apple.com/documentation/metal/hello_triangleTo get it to run I had to add the following line to the renderer class. Any idea why it's not needed in the Objective-C version? pipelineStateDescriptor.depthAttachmentPixelFormat = metalView.depthStencilPixelFormatWithout it I get an error: -[MTLDebugRenderCommandEncoder validateFramebufferWithRenderPipelineState:]:1232: failed assertion `For depth attachment, the render pipeline's pixelFormat (MTLPixelFormatInvalid) does not match the framebuffer's pixelFormat (MTLPixelFormatDepth32Float).'Rob
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
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()
}
}
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?
I've been working on an iOS app with UIDocuments. They were saving fine. Now all of a sudden, after unrelated changes, nothing will save.
Here is the error printed in Xcode console:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL URLByAppendingPathExtension:]: component, components, or pathExtension cannot be nil.'
log.debug("save to:\(self.document.fileURL)")
document.save(to: document.fileURL, for: .forOverwriting) { success in
...
}
The log message there prints a valid URL.
I have developer documentation that contains links that are now dead. How can I find the post that this link used to refer to?
https://forums.developer.apple.com/message/281700
I tried simply using the ID in the new URL format, but that didn't work. Eg:
https://developer.apple.com/forums/thread/281700
Here's a simple demo. I run it on macOS 12.1, compiled with Xcode 13.2.
struct ContentView: View {
var body: some View {
VStack {
Text("ee")
Text("eé")
Text("e\u{E9}") // "e with acute"
Text("ee\u{301}") // "combining acute"
}.font(.custom("Avenir", fixedSize: 18))
.padding(20)
}
}
In the 4rd one, the "e" is rendered in the wrong size/font. Screenshot:
Other fonts do not have the problem. For example, "Avenir Next" and "Helvetica".
Is there a way (in code) to tell which fonts can handle combining chars?
Is this a bug?
I notice the same thing happens when I use Core Text to draw the strings at a low level. So it's not just SwiftUI.
In TextEdit, if have Avenir font, type "option-e" + "e" I get a nice letter. Maybe TextEdit is doing what Xcode did in the second line, and using the "e with acute" unicode character.
The sample code here, has code like:
// Create a display link capable of being used with all active displays
cvReturn = CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink);
But that function's doc says it's deprecated and to use NSView/NSWindow/NSScreen displayLink instead. That returns CADisplayLink, not CVDisplayLink.
Also the documentation for that displayLink method is completely empty. I'm not sure if I'm supposed to add it to run loop, or what, after I get it.
It would be nice to get an updated version of this sample project and/or have some documentation in NSView.displayLink
Topic:
Graphics & Games
SubTopic:
Metal
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
I'd like to use the new localization screenshots and test plans feature to take screenshots in different languages, for the app store. I end up with images that are half one screen and half another, like it's some kind of timing issue. My test code is below. Is it missing something that would give it the right timing on the screenshots?I wrote a UI test and set "Localization Screenshots" to "On" in the test plan's settings. The UI test walks through a few screens and the resulting test report has a few image files attached labeled "Localization screenshot". Some images are are split so that the left side is one view controller and the right side is another, like it captured a push navigation transition. Another image has two overlaid screens, each half faded.I'm running in the simulator. My test code looks like: func testTakeScreenshots() {
let app = XCUIApplication()
app.launch()
// At workouts page
app.tables["workouts"].cells.element(boundBy: 0).tap()
// At first workout
app.navigationBars.buttons["edit"].tap()
// At workout edit screen, click first exercise
app.tables.cells.element(boundBy: 0).tap()
...
}
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?