Post

Replies

Boosts

Views

Activity

SwiftUI: List selection of custom struct
I want to have a single selection in a List, holding content of a custom struct: struct ChannelInfo: Hashable {     let title: String     let description: String } And the view: struct JoinChannelView: View {     @Binding var rooms: [ChannelInfo]     @State var selectedRoom: String?     var body: some View {         VStack {             if $rooms.isEmpty {                 Text("Loading...")                     .transition(.slide)             } else {                 List(selection: $selectedRoom) {                     ForEach(rooms, id: \.title) { room in                         Text(room.title)                             .font(.title)                         Text(room.description)                             .font(.subheadline)                     }                     .padding(.bottom)                 }             }         }         .frame(width: 150, height: 300)     } } My problem here is, that because inside the ForEach I render two Text views, both are selectable, but that's incorrect, I only want the title, the first Text to be selectable.
2
0
1.5k
Sep ’22
SwiftUI: How to animate List when being populated
I want to display a simple message while waiting for a list to be populated. Something like Loading... and when the list starts being populated with even one item, remove the message, via a transition, and display the list. My current attempt, that does not work, looks like: struct JoinChannelView: View {     @Binding var rooms: [ChannelInfo]     @State var selectedRoom: String?     var body: some View {         VStack {             if $rooms.isEmpty {                 Text("Loading...")                     .transition(.slide)             } else {                 List(selection: $selectedRoom) {                     ForEach(rooms, id: \.title) { room in                         Text(room.title)                             .font(.title)                         Text(room.description)                             .font(.subheadline)                     }                     .padding(.bottom)                 }             }         }         .frame(width: 150, height: 300)     } }
1
0
4.9k
Sep ’22
Does code at 04:41 compile?
The code for @State doesn't seem to work. struct DonutListView: View { var donutList: DonutList @State private var donutToAdd: Donut? var body: some View { List(donutList.donuts) { DonutView(donut: $0) } Button("Add Donut") { donutToAdd = Donut() } .sheet(item: $donutToAdd) { // <-- would need a "donut in" TextField("Name", text: $donutToAdd.name) // <-- donutToAdd is optional and I'm not sure how it would be unwrapped Button("Save") { donutList.donuts.append(donutToAdd) donutToAdd = nil } Button("Cancel") { donutToAdd = nil } } } } Does anyone have a fix for this? Thanks, Dan!
1
2
819
Jun ’23
How to create and advertise a Bonjour service with Network.framework?
I'm trying to create and advertise a Bonjour service via Network.framework. In Xcode, target, Info tab, I've added the entry to plist: And then written the following code: guard let listener = try? NWListener(service: .init(name: "My Printer Service", type: "_printer._tcp"),using: .tcp) else { return nil } self.listener = listener listener.stateUpdateHandler = { newState in switch newState { case.ready: print("starting") case .failed(let error): print("error: \(error)") default: print(newState) } } listener.start(queue: .main) However, the status is failed with error message: POSIXErrorCode(rawValue: 22): Invalid argument
1
0
973
Nov ’23