Post

Replies

Boosts

Views

Activity

Misleading error on ForEach
During refactoring of an app I made a typo which leads to a misleading error message in Xcode 26.4. I could reproduce it with a small sample code in Swift Playground. Is it a bug which should be reported? Details: I have an array containing two strings. Using a ForEach loop is fine: ForEach(appData.dataArray, id: \.self) { value in Text("\(value.subject)\t\(value.room)") } but with a typo in the Text line I got an error on the ForEach line: ForEach(appData.dataArray, id: \.self) { value in --> Cannot convert value of type '[MyArray]' to expected argument type 'Binding' Text("\(value.subject)\t\(value.subject.room)") } Complete sample code from Swift Playground (macOS 26): import SwiftUI class MyArray : Hashable, Equatable, Identifiable, ObservableObject, Codable { let id = UUID() @Published var subject: String @Published var room : String private enum CodingKeys : String, CodingKey { case subject case room } init(subject : String, room : String) { self.subject = subject self.room = room } func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(subject, forKey: .subject) try container.encode(room, forKey: .room) } required init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) subject = try container.decode(String.self, forKey: .subject) room = try container.decode(String.self, forKey: .room) } static func == (v1: MyArray, v2: MyArray) -> Bool { let result = v1.id == v2.id return result } func hash(into hasher: inout Hasher) { hasher.combine(id) } } public class AppData : ObservableObject { @Published var dataArray : [MyArray] = [] init() { dataArray.append(MyArray(subject: "Foo", room: "Bar")) dataArray.append(MyArray(subject: "Foo", room: "Batz")) } } struct ContentView: View { @EnvironmentObject var appData : AppData var body: some View { ForEach(appData.dataArray, id: \.self) { value in Text("\(value.subject)\t\(value.subject.room)") // to fix the error replace value.subject.room with value.room } } } @main struct MyApp: App { var appData = AppData() var body: some Scene { WindowGroup { ContentView() .environmentObject(appData) } } }
2
0
1.3k
1w
swift: Calling "/usr/bin/defaults" returns no data
I'd like to create a small helper app for new students do read/write User default settings. Since it was not possible using the UserDefaults class I decided to use the "/usr/bin/defaults". Unfortuntely it seems not to return anything. Debug output shows "Got data: 0 bytes" Here is a sample code: import SwiftUI func readDefaults(domain : String, key :String) -> String { let cmdPath = "/usr/bin/defaults" //let cmdPath = "/bin/ls" let cmd = Process() let pipe = Pipe() cmd.standardOutput = pipe cmd.standardError = pipe cmd.executableURL = URL(fileURLWithPath: cmdPath, isDirectory: false, relativeTo: nil) cmd.arguments = ["read", domain, key] //cmd.arguments = ["/", "/Library"] print("Shell command: \(cmdPath) \(cmd.arguments?.joined(separator: " ") ?? "")") var d : Data? do { try cmd.run() d = pipe.fileHandleForReading.readDataToEndOfFile() cmd.waitUntilExit() } catch let e as NSError { return "ERROR \(e.code): \(e.localizedDescription)" } catch { return "ERROR: call failed!" } // get pipe output and write is to stdout guard let d else { return "ERROR: Can't get pipe output from command!" } print("Got data: \(d)") if let s = String(data: d, encoding: String.Encoding.utf8) { print("Got result: \(s)") return s } else { return "ERROR: No output from pipe." } } struct ContentView: View { let foo = readDefaults(domain: "com.apple.Finder", key: "ShowHardDrivesOnDesktop") var body: some View { VStack { Text("ShowHardDrivesOnDesktop: \(foo.description)") } .padding() } } #Preview { ContentView() } This code works well e.g. for "ls" when the comments are changed for cmdPath and cmd.arguments. What do I miss in order to get it working with defaults?
5
0
185
Mar ’26
Drag & Drop with view hierarchy
Hi! I wrote a SwiftUI based app which does use a layout similar to a calendar. A week view with 5 day-views. Each day view may contain several record views. I've implemented a drag for the records which works well inside a day view (up and down) but I can't get it working between the days. Here is an example how it looks: Any idea or example how to get the proper coordinate information related to the week view inside the rectangle view? A GeometryReader inside the Rectangle view does not give me the needed information. I have currently no idea how to implement it properly…
Topic: UI Frameworks SubTopic: SwiftUI
1
0
104
Nov ’25
Misleading error on ForEach
During refactoring of an app I made a typo which leads to a misleading error message in Xcode 26.4. I could reproduce it with a small sample code in Swift Playground. Is it a bug which should be reported? Details: I have an array containing two strings. Using a ForEach loop is fine: ForEach(appData.dataArray, id: \.self) { value in Text("\(value.subject)\t\(value.room)") } but with a typo in the Text line I got an error on the ForEach line: ForEach(appData.dataArray, id: \.self) { value in --> Cannot convert value of type '[MyArray]' to expected argument type 'Binding' Text("\(value.subject)\t\(value.subject.room)") } Complete sample code from Swift Playground (macOS 26): import SwiftUI class MyArray : Hashable, Equatable, Identifiable, ObservableObject, Codable { let id = UUID() @Published var subject: String @Published var room : String private enum CodingKeys : String, CodingKey { case subject case room } init(subject : String, room : String) { self.subject = subject self.room = room } func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(subject, forKey: .subject) try container.encode(room, forKey: .room) } required init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) subject = try container.decode(String.self, forKey: .subject) room = try container.decode(String.self, forKey: .room) } static func == (v1: MyArray, v2: MyArray) -> Bool { let result = v1.id == v2.id return result } func hash(into hasher: inout Hasher) { hasher.combine(id) } } public class AppData : ObservableObject { @Published var dataArray : [MyArray] = [] init() { dataArray.append(MyArray(subject: "Foo", room: "Bar")) dataArray.append(MyArray(subject: "Foo", room: "Batz")) } } struct ContentView: View { @EnvironmentObject var appData : AppData var body: some View { ForEach(appData.dataArray, id: \.self) { value in Text("\(value.subject)\t\(value.subject.room)") // to fix the error replace value.subject.room with value.room } } } @main struct MyApp: App { var appData = AppData() var body: some Scene { WindowGroup { ContentView() .environmentObject(appData) } } }
Replies
2
Boosts
0
Views
1.3k
Activity
1w
swift: Calling "/usr/bin/defaults" returns no data
I'd like to create a small helper app for new students do read/write User default settings. Since it was not possible using the UserDefaults class I decided to use the "/usr/bin/defaults". Unfortuntely it seems not to return anything. Debug output shows "Got data: 0 bytes" Here is a sample code: import SwiftUI func readDefaults(domain : String, key :String) -> String { let cmdPath = "/usr/bin/defaults" //let cmdPath = "/bin/ls" let cmd = Process() let pipe = Pipe() cmd.standardOutput = pipe cmd.standardError = pipe cmd.executableURL = URL(fileURLWithPath: cmdPath, isDirectory: false, relativeTo: nil) cmd.arguments = ["read", domain, key] //cmd.arguments = ["/", "/Library"] print("Shell command: \(cmdPath) \(cmd.arguments?.joined(separator: " ") ?? "")") var d : Data? do { try cmd.run() d = pipe.fileHandleForReading.readDataToEndOfFile() cmd.waitUntilExit() } catch let e as NSError { return "ERROR \(e.code): \(e.localizedDescription)" } catch { return "ERROR: call failed!" } // get pipe output and write is to stdout guard let d else { return "ERROR: Can't get pipe output from command!" } print("Got data: \(d)") if let s = String(data: d, encoding: String.Encoding.utf8) { print("Got result: \(s)") return s } else { return "ERROR: No output from pipe." } } struct ContentView: View { let foo = readDefaults(domain: "com.apple.Finder", key: "ShowHardDrivesOnDesktop") var body: some View { VStack { Text("ShowHardDrivesOnDesktop: \(foo.description)") } .padding() } } #Preview { ContentView() } This code works well e.g. for "ls" when the comments are changed for cmdPath and cmd.arguments. What do I miss in order to get it working with defaults?
Replies
5
Boosts
0
Views
185
Activity
Mar ’26
Drag & Drop with view hierarchy
Hi! I wrote a SwiftUI based app which does use a layout similar to a calendar. A week view with 5 day-views. Each day view may contain several record views. I've implemented a drag for the records which works well inside a day view (up and down) but I can't get it working between the days. Here is an example how it looks: Any idea or example how to get the proper coordinate information related to the week view inside the rectangle view? A GeometryReader inside the Rectangle view does not give me the needed information. I have currently no idea how to implement it properly…
Topic: UI Frameworks SubTopic: SwiftUI
Replies
1
Boosts
0
Views
104
Activity
Nov ’25