Post

Replies

Boosts

Views

Activity

Reply to Is packet order guaranteed in Bluetooth?
Packets are numbered and the protocol checks they all arrived before considering the transmission complete. So at the application level, they should be in correct order. Some interesting docs: https://stackoverflow.com/questions/77674630/does-the-ble-protocol-guarantee-write-ordering-across-multiple-characteristics-i https://stackoverflow.com/questions/77674630/does-the-ble-protocol-guarantee-write-ordering-across-multiple-characteristics-i Hope that helps.
Mar ’24
Reply to List half of an array
Here it is. It is a little bit verbose to make it easier to understand. Don't forget to close the thread if that works. Otherwise, explain what is the problem. And do the same on your older threads to keep forum clean. struct Squadre: Identifiable, Equatable { // ad hoc, for testing. Adapt to yours. var id = UUID() var squadra: String var punti: Int static func test() -> [Squadre] { [Squadre(squadra: "One", punti: 1), Squadre(squadra: "Seven", punti: 7), Squadre(squadra: "Three", punti: 3), Squadre(squadra: "Nine", punti: 9), Squadre(squadra: "Twelve", punti: 12), Squadre(squadra: "Five", punti: 5), Squadre(squadra: "Four", punti: 4)] } } struct ContentView: View { @State private var squadre: [Squadre] = Squadre.test() @State private var firstHalfSquadre: [Squadre] = [] @State private var secondHalfSquadre: [Squadre] = [] var body: some View { HStack { List { ForEach($firstHalfSquadre, id: \.id) { $sq in // do not reuse squadre as name HStack { Text(sq.squadra) Text("\(sq.punti)") } } } List { ForEach($secondHalfSquadre, id: \.id) { $sq in // do not reuse squadre as name HStack { Text(sq.squadra) Text("\(sq.punti)") } } } } .onAppear { let lastHalfCount = squadre.count / 2 let firstHalfCount = squadre.count - lastHalfCount // to take care if count is odd firstHalfSquadre = squadre.sorted(by: { $0.punti > $1.punti } ) // decreasing order firstHalfSquadre = firstHalfSquadre.dropLast(lastHalfCount) // remove last half secondHalfSquadre = squadre.sorted(by: { $0.punti > $1.punti } ) // decreasing order secondHalfSquadre = Array(secondHalfSquadre.dropFirst(firstHalfCount)) // keep last half // dropFirst creates a subsequence, not an array… So need to convert to Array. } .onChange(of: squadre) { let lastHalfCount = squadre.count / 2 let firstHalfCount = squadre.count - lastHalfCount firstHalfSquadre = squadre.sorted(by: { $0.punti > $1.punti } ).dropLast(lastHalfCount) secondHalfSquadre = Array(squadre.sorted(by: { $0.punti > $1.punti } ).dropFirst(firstHalfCount)) } // To play with it… Button(action: { squadre.append(Squadre(squadra: "Two", punti: 2)) }) { Text("Add Punti 2") } Button(action: { squadre[0] = Squadre(squadra: "New One", punti: 10) }) { Text("Modify first \(squadre[0].squadra) to 10") } Button(action: { let newValue = squadre[0].punti + 5 squadre[0] = Squadre(squadra: "New first", punti: newValue) }) { Text("Add 5 to first") } } }
Topic: Programming Languages SubTopic: Swift Tags:
Mar ’24
Reply to List half of an array
You could build a new array @State private var halfSquadre: [Squadre] = [] Then at some point (for instance .onAppear), initialise it halfSquadre = squadre.sorted(by: { $0.punti < $1.punti } ) // increasing order let halfCount = squadre.count / 2 // take care if count is uneven : if 11, do you want to display 5 or 6 ? halfSquadre = halfSquadre.dropLast(halfCount) // remove last half Use it in ForEach: List { ForEach(halfSquadre) { squadre in HStack { Text(squadre.squadra) Text("\(squadre.punti)") } } } If you want to update dynamically, use onChange. Note that I created a Squadre struct for the test. struct Squadre: Identifiable, Equatable { var id = UUID() var squadra: String var punti: Int static func test() -> [Squadre] { [Squadre(squadra: "One", punti: 1), Squadre(squadra: "Seven", punti: 7), Squadre(squadra: "Three", punti: 3), Squadre(squadra: "Nine", punti: 9), Squadre(squadra: "Twelve", punti: 12), Squadre(squadra: "Five", punti: 5), Squadre(squadra: "Four", punti: 4)] } } struct ContentView: View { @State private var squadre: [Squadre] = Squadre.test() @State private var halfSquadre: [Squadre] = [] var body: some View { VStack { List { ForEach($halfSquadre, id: \.id) { $sq in // do not reuse squadre as name HStack { Text(sq.squadra) Text("\(sq.punti)") } } } .onAppear { halfSquadre = squadre.sorted(by: { $0.punti < $1.punti } ) // increasing order let halfCount = squadre.count / 2 // take care if count is uneven : if 11, do you want to display 5 or 6 ? halfSquadre = halfSquadre.dropLast(halfCount) // remove last half } .onChange(of: squadre) { halfSquadre = squadre.sorted(by: { $0.punti < $1.punti } ).dropLast(squadre.count / 2) } Button(action: { squadre.append(Squadre(squadra: "Two", punti: 2)) }) { Text("Add Punti 2") } Button(action: { squadre[0] = Squadre(squadra: "Ten", punti: 10) }) { Text("Modify first to 10") } } } }
Topic: Programming Languages SubTopic: Swift Tags:
Mar ’24
Reply to Type 'MyApp' does not conform to protocol 'App'
I have the following code in my ContentView.swift file: Is it really in ContentView file ? It should be in a MyApp file. I tested by creating the same app. Here is the structure you should get when you create a VisionOS project (called My): ContentView File is import SwiftUI import RealityKit import RealityKitContent struct ContentView: View { var body: some View { VStack { Model3D(named: "Scene", bundle: realityKitContentBundle) .padding(.bottom, 50) Text("Hello, world!") } .padding() } } #Preview(windowStyle: .automatic) { ContentView() }
Topic: App & System Services SubTopic: Core OS Tags:
Mar ’24
Reply to SwiftUI UI auto refresh problem
Problem is that list items are not observed, hence, no change in State var when updating. To see it, just change the state var count and restore in setAllType(): func setAllType(on: Int) { for type in list { type.count = on } count += 1 count -= 1 } As is, it works.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’24