Post

Replies

Boosts

Views

Activity

Crash when triggering sheet presentation from a task
Given the following code, if line 17 isn't present there'll be a crash upon presenting the sheet. Since content shouldn't be nil at the time of presentation, why does this crash? import SwiftUI struct ContentView: View { @State private var isPresented = false @State private var content: String? @State private var startTask = false var body: some View { Text("Tap me") .onTapGesture { startTask = true } .task(id: startTask) { guard startTask else { return } startTask = false // <===== Crashes if removed content = "Some message" isPresented = true } .sheet(isPresented: $isPresented) { Text(content!) } } } iOS 16.4, Xcode 14.3.1
6
0
998
Aug ’23
SwiftUI LibraryItem instances can produce unexpected results
When passing an empty closure to a LibraryItem's snippet parameter, unexpected extra text is added to the file when the item is dragged in from the Xcode library. Expected: .setAction { } Actual: .setAction /*@START_MENU_TOKEN@*/{ }/*@END_MENU_TOKEN@*/ Full code: import SwiftUI struct ContentView: View {   var body: some View {     MyView()   } } struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } } struct MyView: View {   var action: (() -> Void)?       var body: some View {     Text("MyView")       .onTapGesture {         action?()       }   }       func setAction(_ action: @escaping () -> Void) -> MyView {     var copy = self     copy.action = action     return copy   } } struct LibraryContent: LibraryContentProvider {   func modifiers(base: MyView) -> [LibraryItem] {     [       LibraryItem(         base.setAction {                     },         title: "MyView - Set action"       )     ]   } } FB11936092 macOS 13.0.1 (22A400) Xcode 14.1 (14B47b)
0
0
666
Jan ’23