Post

Replies

Boosts

Views

Activity

SwiftUI Table with TextField becomes unresponsive as entries increase
I am trying to implement a simple Table with a TextField that makes it possible to edit the data. Below is a very simple example where I am trying to do this. It works, but it becomes very unresponsive as the number of people increases. The example as it is, has 1,000 people. It is very slow when a TextField gets focus and is used to edit something. Strangely, it also gets very slow when you scroll through an entry that has been edited. It crashes completely when 10,000 people is used. I found this similar question on the web, but it is not answered. What should I do to implement an editable Table like this? struct ContentView: View { struct Person: Identifiable { var givenName: String var familyName: String let id = UUID() } @State private var people = [Person].init(repeating: Person(givenName: "Name", familyName: "Family"), count: 1000) var body: some View { Table($people) { TableColumn("Given Name") { $person in TextField("Name", text: $person.givenName) } TableColumn("Family Name") { $person in TextField("Family Name", text: $person.familyName) } TableColumn("Full Name") { $person in Text("\(person.givenName) \(person.familyName)")} } } }
1
3
2.2k
Oct ’23
How to preserve TextField cursor position when replacing text
I am doing text replacement in a SwiftUI TextField as the user types. This works fine when the cursor is at the end of the input, but fails when the user moves the cursor to the middle and start typing. As soon as a correction is made, the cursor jumps to the end of the string moving the user's cursor away from where they were editing. Below is a simple example of a view doing text replacement. Is it possible to get the cursor position inside the onChange modifier, and preserve it without custom wrapping a UITextView. I've done that, and it creates other problems. struct ContentView: View { @State private var input: String = "" var body: some View { VStack { TextField("", text: $input) .onChange(of: input) { input = input.replacingOccurrences(of: "*", with: "×") input = input.replacingOccurrences(of: "/", with: "÷") input = input.replacingOccurrences(of: "pi", with: "π") } } .padding() } }
0
2
1.5k
Feb ’24
How to get SwiftUI views to move, not fade when animating change
I am trying to get SwiftUI views to move from one position to another using animation. I know that when the identity of the views change this often result in a fade instead of an animation but I do not believe this is happening in my case. Below is a simple example program with four views from a 2D array (the 2D array is important). There is a button to rotate the views clockwise. I tried using two ForEach blocks but I cannot use it on the outer one because the row arrays does not conform to Identifiable. I also changed my code to write out the views explicitly without the ForEach blocks but I get the same result: The column changes animate as movement but the row changes animate as fades. How do I get it to animate all four views moving clockwise? import SwiftUI struct Block: Identifiable { var id : Int = 0 } struct Board { let numRows = 2 let numCols = 2 var blocks: [[Block]] init() { blocks = Array(repeating: Array(repeating: Block(), count: numCols), count: numRows) for row in 0..<numRows { for col in 0..<numCols { blocks[row][col].id = row * numCols + col } } } mutating func rotate() { let temp = blocks[0][0] blocks[0][0] = blocks[1][0] blocks[1][0] = blocks[1][1] blocks[1][1] = blocks[0][1] blocks[0][1] = temp } } struct BlockView: View { var block: Block var body: some View { ZStack { Rectangle() .fill(Color.secondary) Text("\(block.id)") .font(.largeTitle) } .aspectRatio(1.0, contentMode: .fit) } } struct ContentView: View { @State var board = Board() var body: some View { VStack { Button("Rotate") { withAnimation(.easeInOut(duration: 1.0)) { board.rotate() } } VStack { ForEach(0..<2) { row in HStack { ForEach(board.blocks[row]) { block in BlockView(block: block) } } } } } .padding() } } #Preview { ContentView() }```
0
0
508
Mar ’24
Why does custom Binding not update UI
I have a class that I cannot change to ObservableObject with Published members. I tried getting around this by writing my own Binding. Although the value is updated correctly, the UI is not. Why is this. Below is a simple demo view. When it is run and the toggle is clicked, it will print out correctly that the value is changed, but the UI does not update. Why? import SwiftUI class BoolWrapper {   public var value = false {     didSet {       print("Value changed to \(value)")     }   } } let boolWrapper = BoolWrapper() struct ContentView: View {   var body: some View {     Toggle(isOn: Binding(get: {       return boolWrapper.value     }, set: { value in       boolWrapper.value = value     }), label: { Text("Toggle") })   } } struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } }
5
0
4.9k
Mar ’24
How to use network sockets with async/await?
I have an application that communicates with custom external hardware on the network (using UDP). I have a thread that receives and process the UDP data and then signals a waiting thread by releasing a semaphore when data is available. A have a asyncSendAndReceive and asyncReceive function that just begs to use async/await. But I cannot simply switch because of the use of the semaphore. Various forums and discussions said that semaphores should no longer be used for signalling. If not semaphores, then what else? Note that my two async functions may not always block. If data was received before they were called, then it is queued (and the semaphore is signalled).
9
0
3.8k
Jul ’24
Using TextField:text:selection crashes on macOS
I am trying out the new TextField selection ability on macOS but it crashes in various different ways with extremely large stack traces. Looks like it is getting into re-entrant function calls. A similar problem is described on the SwiftUI forums with no responses yet. Here is my simple example struct ContentView: View { @State private var text: String = "" @State private var selection: TextSelection? var body: some View { TextField("Message", text: $text, selection: $selection) .padding() } } Setting text to a value like "Hallo World" causes an instant crash as soon as you start typing in the TextField. Setting text empty (as in example above) lets you edit the text but as it crashes as soon as you commit it (press enter). Any workarounds or fixes?
4
1
570
Dec ’24
Why is AVAudioEngine input giving all zero samples?
I am trying to get access to raw audio samples from mic. I've written a simple example application that writes the values to a text file. Below is my sample application. All the input samples from the buffers connected to the input tap is zero. What am I doing wrong? I did add the Privacy - Microphone Usage Description key to my application target properties and I am allowing microphone access when the application launches. I do find it strange that I have to provide permission every time even though in Settings > Privacy, my application is listed as one of the applications allowed to access the microphone. class AudioRecorder { private let audioEngine = AVAudioEngine() private var fileHandle: FileHandle? func startRecording() { let inputNode = audioEngine.inputNode let audioFormat: AVAudioFormat #if os(iOS) let hardwareSampleRate = AVAudioSession.sharedInstance().sampleRate audioFormat = AVAudioFormat(standardFormatWithSampleRate: hardwareSampleRate, channels: 1)! #elseif os(macOS) audioFormat = inputNode.inputFormat(forBus: 0) // Use input node's current format #endif setupTextFile() inputNode.installTap(onBus: 0, bufferSize: 1024, format: audioFormat) { [weak self] buffer, _ in self!.processAudioBuffer(buffer: buffer) } do { try audioEngine.start() print("Recording started with format: \(audioFormat)") } catch { print("Failed to start audio engine: \(error.localizedDescription)") } } func stopRecording() { audioEngine.stop() audioEngine.inputNode.removeTap(onBus: 0) print("Recording stopped.") } private func setupTextFile() { let tempDir = FileManager.default.temporaryDirectory let textFileURL = tempDir.appendingPathComponent("audioData.txt") FileManager.default.createFile(atPath: textFileURL.path, contents: nil, attributes: nil) fileHandle = try? FileHandle(forWritingTo: textFileURL) } private func processAudioBuffer(buffer: AVAudioPCMBuffer) { guard let channelData = buffer.floatChannelData else { return } let channelSamples = channelData[0] let frameLength = Int(buffer.frameLength) var textData = "" var allZero = true for i in 0..<frameLength { let sample = channelSamples[i] if sample != 0 { allZero = false } textData += "\(sample)\n" } if allZero { print("Got \(frameLength) worth of audio data on \(buffer.stride) channels. All data is zero.") } else { print("Got \(frameLength) worth of audio data on \(buffer.stride) channels.") } // Write to file if let data = textData.data(using: .utf8) { fileHandle!.write(data) } } }
4
0
1k
Jan ’25
SwiftUI TextField corrupts selection when inserting utf16
The example code below shows what I am trying to achieve: When the user types a '*', it should be replaced with a '×'. It looks like it works, but the cursor position is corrupted, even though it looks OK, and the diagnostics that is printed below shows a valid index. If you type "12*34" you get "12×43" because the cursor is inserting before the shown cursor instead of after. How can I fix this? struct ContentView: View { @State private var input: String = "" @State private var selection: TextSelection? = nil var body: some View { VStack { TextField("Type 12*34", text: $input, selection: $selection) .onKeyPress(action: {keyPress in handleKeyPress(keyPress) }) Text("Selection: \(selectionAsString())") }.padding() } func handleKeyPress(_ keyPress: KeyPress) -> KeyPress.Result { if (keyPress.key.character == "*") { insertAtCursor(text: "×") moveCursor(offset: 1) return KeyPress.Result.handled } return KeyPress.Result.ignored } func moveCursor(offset: Int) { guard let selection else { return } if case let .selection(range) = selection.indices { print("Moving cursor from \(range.lowerBound)") let newIndex = input.index(range.lowerBound, offsetBy: offset, limitedBy: input.endIndex)! let newSelection : TextSelection.Indices = .selection(newIndex..<newIndex) if case let .selection(range) = newSelection { print("Moved to \(range.lowerBound)") } self.selection!.indices = newSelection } } func insertAtCursor(text: String) { guard let selection else { return } if case let .selection(range) = selection.indices { input.insert(contentsOf: text, at: range.lowerBound) } } func selectionAsString() -> String { guard let selection else { return "None" } switch selection.indices { case .selection(let range): if (range.lowerBound == range.upperBound) { return ("No selection, cursor at \(range.lowerBound)") } let lower = range.lowerBound.utf16Offset(in: input) let upper = range.upperBound.utf16Offset(in: input) return "\(lower) - \(upper)" case .multiSelection(let rangeSet): return "Multi selection \(rangeSet)" @unknown default: fatalError("Unknown selection") } } }
Topic: UI Frameworks SubTopic: SwiftUI
7
0
180
Sep ’25
SwiftUI Table with TextField becomes unresponsive as entries increase
I am trying to implement a simple Table with a TextField that makes it possible to edit the data. Below is a very simple example where I am trying to do this. It works, but it becomes very unresponsive as the number of people increases. The example as it is, has 1,000 people. It is very slow when a TextField gets focus and is used to edit something. Strangely, it also gets very slow when you scroll through an entry that has been edited. It crashes completely when 10,000 people is used. I found this similar question on the web, but it is not answered. What should I do to implement an editable Table like this? struct ContentView: View { struct Person: Identifiable { var givenName: String var familyName: String let id = UUID() } @State private var people = [Person].init(repeating: Person(givenName: "Name", familyName: "Family"), count: 1000) var body: some View { Table($people) { TableColumn("Given Name") { $person in TextField("Name", text: $person.givenName) } TableColumn("Family Name") { $person in TextField("Family Name", text: $person.familyName) } TableColumn("Full Name") { $person in Text("\(person.givenName) \(person.familyName)")} } } }
Replies
1
Boosts
3
Views
2.2k
Activity
Oct ’23
How to preserve TextField cursor position when replacing text
I am doing text replacement in a SwiftUI TextField as the user types. This works fine when the cursor is at the end of the input, but fails when the user moves the cursor to the middle and start typing. As soon as a correction is made, the cursor jumps to the end of the string moving the user's cursor away from where they were editing. Below is a simple example of a view doing text replacement. Is it possible to get the cursor position inside the onChange modifier, and preserve it without custom wrapping a UITextView. I've done that, and it creates other problems. struct ContentView: View { @State private var input: String = "" var body: some View { VStack { TextField("", text: $input) .onChange(of: input) { input = input.replacingOccurrences(of: "*", with: "×") input = input.replacingOccurrences(of: "/", with: "÷") input = input.replacingOccurrences(of: "pi", with: "π") } } .padding() } }
Replies
0
Boosts
2
Views
1.5k
Activity
Feb ’24
How to get SwiftUI views to move, not fade when animating change
I am trying to get SwiftUI views to move from one position to another using animation. I know that when the identity of the views change this often result in a fade instead of an animation but I do not believe this is happening in my case. Below is a simple example program with four views from a 2D array (the 2D array is important). There is a button to rotate the views clockwise. I tried using two ForEach blocks but I cannot use it on the outer one because the row arrays does not conform to Identifiable. I also changed my code to write out the views explicitly without the ForEach blocks but I get the same result: The column changes animate as movement but the row changes animate as fades. How do I get it to animate all four views moving clockwise? import SwiftUI struct Block: Identifiable { var id : Int = 0 } struct Board { let numRows = 2 let numCols = 2 var blocks: [[Block]] init() { blocks = Array(repeating: Array(repeating: Block(), count: numCols), count: numRows) for row in 0..<numRows { for col in 0..<numCols { blocks[row][col].id = row * numCols + col } } } mutating func rotate() { let temp = blocks[0][0] blocks[0][0] = blocks[1][0] blocks[1][0] = blocks[1][1] blocks[1][1] = blocks[0][1] blocks[0][1] = temp } } struct BlockView: View { var block: Block var body: some View { ZStack { Rectangle() .fill(Color.secondary) Text("\(block.id)") .font(.largeTitle) } .aspectRatio(1.0, contentMode: .fit) } } struct ContentView: View { @State var board = Board() var body: some View { VStack { Button("Rotate") { withAnimation(.easeInOut(duration: 1.0)) { board.rotate() } } VStack { ForEach(0..<2) { row in HStack { ForEach(board.blocks[row]) { block in BlockView(block: block) } } } } } .padding() } } #Preview { ContentView() }```
Replies
0
Boosts
0
Views
508
Activity
Mar ’24
Why does custom Binding not update UI
I have a class that I cannot change to ObservableObject with Published members. I tried getting around this by writing my own Binding. Although the value is updated correctly, the UI is not. Why is this. Below is a simple demo view. When it is run and the toggle is clicked, it will print out correctly that the value is changed, but the UI does not update. Why? import SwiftUI class BoolWrapper {   public var value = false {     didSet {       print("Value changed to \(value)")     }   } } let boolWrapper = BoolWrapper() struct ContentView: View {   var body: some View {     Toggle(isOn: Binding(get: {       return boolWrapper.value     }, set: { value in       boolWrapper.value = value     }), label: { Text("Toggle") })   } } struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } }
Replies
5
Boosts
0
Views
4.9k
Activity
Mar ’24
How to use network sockets with async/await?
I have an application that communicates with custom external hardware on the network (using UDP). I have a thread that receives and process the UDP data and then signals a waiting thread by releasing a semaphore when data is available. A have a asyncSendAndReceive and asyncReceive function that just begs to use async/await. But I cannot simply switch because of the use of the semaphore. Various forums and discussions said that semaphores should no longer be used for signalling. If not semaphores, then what else? Note that my two async functions may not always block. If data was received before they were called, then it is queued (and the semaphore is signalled).
Replies
9
Boosts
0
Views
3.8k
Activity
Jul ’24
Using TextField:text:selection crashes on macOS
I am trying out the new TextField selection ability on macOS but it crashes in various different ways with extremely large stack traces. Looks like it is getting into re-entrant function calls. A similar problem is described on the SwiftUI forums with no responses yet. Here is my simple example struct ContentView: View { @State private var text: String = "" @State private var selection: TextSelection? var body: some View { TextField("Message", text: $text, selection: $selection) .padding() } } Setting text to a value like "Hallo World" causes an instant crash as soon as you start typing in the TextField. Setting text empty (as in example above) lets you edit the text but as it crashes as soon as you commit it (press enter). Any workarounds or fixes?
Replies
4
Boosts
1
Views
570
Activity
Dec ’24
Why is AVAudioEngine input giving all zero samples?
I am trying to get access to raw audio samples from mic. I've written a simple example application that writes the values to a text file. Below is my sample application. All the input samples from the buffers connected to the input tap is zero. What am I doing wrong? I did add the Privacy - Microphone Usage Description key to my application target properties and I am allowing microphone access when the application launches. I do find it strange that I have to provide permission every time even though in Settings > Privacy, my application is listed as one of the applications allowed to access the microphone. class AudioRecorder { private let audioEngine = AVAudioEngine() private var fileHandle: FileHandle? func startRecording() { let inputNode = audioEngine.inputNode let audioFormat: AVAudioFormat #if os(iOS) let hardwareSampleRate = AVAudioSession.sharedInstance().sampleRate audioFormat = AVAudioFormat(standardFormatWithSampleRate: hardwareSampleRate, channels: 1)! #elseif os(macOS) audioFormat = inputNode.inputFormat(forBus: 0) // Use input node's current format #endif setupTextFile() inputNode.installTap(onBus: 0, bufferSize: 1024, format: audioFormat) { [weak self] buffer, _ in self!.processAudioBuffer(buffer: buffer) } do { try audioEngine.start() print("Recording started with format: \(audioFormat)") } catch { print("Failed to start audio engine: \(error.localizedDescription)") } } func stopRecording() { audioEngine.stop() audioEngine.inputNode.removeTap(onBus: 0) print("Recording stopped.") } private func setupTextFile() { let tempDir = FileManager.default.temporaryDirectory let textFileURL = tempDir.appendingPathComponent("audioData.txt") FileManager.default.createFile(atPath: textFileURL.path, contents: nil, attributes: nil) fileHandle = try? FileHandle(forWritingTo: textFileURL) } private func processAudioBuffer(buffer: AVAudioPCMBuffer) { guard let channelData = buffer.floatChannelData else { return } let channelSamples = channelData[0] let frameLength = Int(buffer.frameLength) var textData = "" var allZero = true for i in 0..<frameLength { let sample = channelSamples[i] if sample != 0 { allZero = false } textData += "\(sample)\n" } if allZero { print("Got \(frameLength) worth of audio data on \(buffer.stride) channels. All data is zero.") } else { print("Got \(frameLength) worth of audio data on \(buffer.stride) channels.") } // Write to file if let data = textData.data(using: .utf8) { fileHandle!.write(data) } } }
Replies
4
Boosts
0
Views
1k
Activity
Jan ’25
SwiftUI TextField corrupts selection when inserting utf16
The example code below shows what I am trying to achieve: When the user types a '*', it should be replaced with a '×'. It looks like it works, but the cursor position is corrupted, even though it looks OK, and the diagnostics that is printed below shows a valid index. If you type "12*34" you get "12×43" because the cursor is inserting before the shown cursor instead of after. How can I fix this? struct ContentView: View { @State private var input: String = "" @State private var selection: TextSelection? = nil var body: some View { VStack { TextField("Type 12*34", text: $input, selection: $selection) .onKeyPress(action: {keyPress in handleKeyPress(keyPress) }) Text("Selection: \(selectionAsString())") }.padding() } func handleKeyPress(_ keyPress: KeyPress) -> KeyPress.Result { if (keyPress.key.character == "*") { insertAtCursor(text: "×") moveCursor(offset: 1) return KeyPress.Result.handled } return KeyPress.Result.ignored } func moveCursor(offset: Int) { guard let selection else { return } if case let .selection(range) = selection.indices { print("Moving cursor from \(range.lowerBound)") let newIndex = input.index(range.lowerBound, offsetBy: offset, limitedBy: input.endIndex)! let newSelection : TextSelection.Indices = .selection(newIndex..<newIndex) if case let .selection(range) = newSelection { print("Moved to \(range.lowerBound)") } self.selection!.indices = newSelection } } func insertAtCursor(text: String) { guard let selection else { return } if case let .selection(range) = selection.indices { input.insert(contentsOf: text, at: range.lowerBound) } } func selectionAsString() -> String { guard let selection else { return "None" } switch selection.indices { case .selection(let range): if (range.lowerBound == range.upperBound) { return ("No selection, cursor at \(range.lowerBound)") } let lower = range.lowerBound.utf16Offset(in: input) let upper = range.upperBound.utf16Offset(in: input) return "\(lower) - \(upper)" case .multiSelection(let rangeSet): return "Multi selection \(rangeSet)" @unknown default: fatalError("Unknown selection") } } }
Topic: UI Frameworks SubTopic: SwiftUI
Replies
7
Boosts
0
Views
180
Activity
Sep ’25