Post

Replies

Boosts

Views

Activity

Why must I wrap a call to call to an async/await method in a Task?
Trying to to convert my old URL functionality to SwiftUI with async/await. While the skeleton below works, am puzzled as to why I must place my call to the method within a Task? When I don't use Task I get the error: Cannot pass function of type '() async -> Void' to parameter expecting synchronous function type From the examples I've seen, this wasn't necessary. So I either I've misunderstood something, or I am doing this incorrectly, and am looking for little but of guidance. Thank you Within my SwiftUI view: Button { Task { let request = xFile.makePostRequest(xUser: xUser, script: "requestTicket.pl") var result = await myNewURL.asyncCall(request: request) print("\(result)") } } From a separate class: class MyNewURL: NSObject, ObservableObject { func asyncCall(request: URLRequest) async -> Int { do { let (data, response) = try await URLSession.shared.data(for: request) guard let httpResponse = response as? HTTPURLResponse else { print("error") return -1 } if httpResponse.statusCode == 200 { ... } } catch { return -2 } return 0 } }
1
0
3.6k
Jul ’23
Is there an .onAppear equivalent that works on individual items in a view, and also gets called on redraws?
Within my SwiftUI view I have multiple view items, buttons, text, etc. Within the view, the user selects a ticket, and then clicks a button to upload it. The app then sends the ticket to my server, where the server takes time to import the ticket. So my SwiftUI app needs to make repeated URL calls depending on how far the server has gotten with the ticket. I thought the code below would work. I update the text to show at what percent the server is at. However, it only works once. I guess I thought that onAppear would work with every refresh since it's in a switch statement. If I'm reading my debugger correctly, SwiftUI recalls which switch statement was called last and therefore it views my code below as a refresh rather than a fresh creation. So is there a modifier that would get fired repeatedly on every view refresh? Or do I have to do all my multiple URL calls from outside the SwiftUI view? Something like onInitialize but for child views (buttons, text, etc.) within the main view? switch myNewURL.stage { case .makeTicket: Text(myNewURL.statusMsg) .padding(.all, 30) case .importing: Text(myNewURL.statusMsg) .onAppear{ Task { try! await Task.sleep(nanoseconds: 7000000000) print ("stage two") let request = xFile.makeGetReq(xUser: xUser, script: "getTicketStat.pl") var result = await myNewURL.asyncCall(request: request, xFile: xFile) } } .padding(.all, 30) case .uploadOriginal: Text(myNewURL.statusMsg) .padding(.all, 30) case .JSONerr: Text(myNewURL.statusMsg) .padding(.all, 30) }
1
0
1.1k
Jul ’23
Can a button call a new view without using NavigationStack/Link/View?
Is it possible to switch to a new View without using NavigationStack or NavigationLink or NavigationView? I know I can do it with a Bool, which either shows the second view, or the first, and then toggles the Bool. But can't I do something like this? Which obviously doesn't work. struct BasicButton: View { var buttonLabel = "Create User" var body: some View { Button { CreateUser() //another SwiftUI view, not a function } label: { Text(buttonLabel) } } }
3
0
922
May ’24
Why doesn't Xcode's "Download Container" not always work?
Been using Xcode's Download Container for a short time, and at first there were no issues. But lately, it doesn't always download all the files from my iPhone to the Mac. Sometimes cleaning the build folder works, other times not. As well, the same happens whether I am connected to my iPhone via cable, or wifi. Has anyone else had this issue?
6
0
407
Nov ’24
Is there a global Alert View in SwiftUI?
I am writing a SwiftUI based app, and errors can occur anywhere. I've got a function that logs the error. But it would be nice to be able to call an Alert Msg, no matter where I am, then gracefully exits the app. Sure I can right the alert into every view, but that seems ridiculously unnecessary. Am I missing something?
2
0
114
Apr ’25
iPhone won't connect to Xcode over WiFi
Hello, Just starting to learn Xcode and I can test the first chapter's app on my iPhone if it's conncted via USB-C. The book walks me through the part where I can allow Xcode to connect to the iPhone via WiFi, just checking "Connect via Network."Yet Xcode cannot find my phone unless it's connected via USB. When I go to Devices that checkbox stays checked, unless I unplug the phone it which case that box doesn't even appear.And yes, they are both on the same WiFi, it's the only one I have and it's up and running.Any thoughs?
6
0
21k
Jun ’23
Does AVAudioPCMBuffer have a "partial copy" method?
I have this long kludgy bit of code that works. I've outlined it below so as not to be confusing as I have no error within my code. I just need to know if there's a method that already exists to copy a specific part of an AVAudioPCMBuffer to a new AVAudioPCMBuffer? So if I have an AVAudioPCMBuffer of 10,000 frames, and I just want frames 500 through 3,000 copied into a new buffer (formatting and all) without altering the old buffer...is there a method to do this? My code detects silent moments in an audio recording I currently read an audio file into an AVAudioPCMBuffer (audBuffOne) I loop through the buffer and detect the starts and ends of silence I record their positions in an array This array holds what frame position I detect voice starting (A) and which frame position the voice ends (B), with some padding of course ... new loop ... I loop through my array to go through each A and B framePositions Using the sample size from the audio file's formatting info, I create a new AVAudioPCMBuffer (audBuffTwo) large enough to hold from A to B and having the same formatting as audBuffOne I go back to audBuffOne Set framePosition to on the audio file to A Read into audBuffTwo for there proper length to reach frame B Save audBuffTwo to a new file ...keep looping
1
1
771
Jan ’23
Xcode debugger seems slow
It just feels as if my debugger is running super slow when I step over each line. Each line is doing string comparison, splitting text into words, really nothing fancy. It appears that every time I hit F6, the Variables View (local variables) takes 4 seconds or more to refresh. But I don't know if that's the cause, or a symptom. Just curious if anyone can shed any light on this. Specs MacBook Pro 2019 2.6 GHz 6-Core Intel Core i7 16 GB 2667 MHz DDR4 Sequoia Version 15.1.1 (24B91) iPhone running app is 13 pro 18.1.1 Xcode Version 16.2 (16C5032a)
1
1
350
Jan ’25
Any solution yet to not being able to turn off debugging via WiFI?
Debugger on Xcode 16.x is super slow and it turns out it's only this way when Xcode is connected to my iPhone via WiFi. If I disable WiFI on my iPhone everything is just fine. But that's not a solution. An engineer posted this supposed solution, https://developer.apple.com/documentation/xcode-release-notes/xcode-15-release-notes. Forgive me but that's not a solution, especially since we used to be able to shut off "Connect via WiFI." I've seen so many posts here and everywhere else with no one stating any clear answer. Does anyone know why has this been removed? And is anyone aware of it? I've posted in the Feedback Asst. as many others have. What gives?
0
1
229
Jan ’25
Latest version of Xcode has weird Preview issues
So I believe my machine JUST updated to Xcode 16.3 (16E140). But it definitely just installed the latest iOS simulator 18.4. However, now my preview will sometimes give me the error Failed to launch app ”Picker.app” in reasonable time. If I add a space in my code, or hit refresh on the Preview, then it will run on the second or third attempt. Sometimes in between the refreshes, the preview will crash, and then it will work again. Anyone else experiencing this? Any ideas? Thanks
1
1
163
Apr ’25
Is there a simple way to adding files to iPhone simulator, for use with Xcode?
Correct me if I'm wrong, but with the latest version of Xcode (15.x) you can no longer add files to the iPhone simulator by dragging them into the the Files app. I also tried to share the files from my Mac desktop to the simulator. But after selecting the simulator, absolutely nothing happened. So I had to do it the long way: Add a folder to the simulator with a unique name, in the Files app Get the document path, print(URL.documentsDirectory.path()) Back track into the folder structure till I find that folder cp the files to that folder Please tell me that there is a way that I haven't found on Google, or that I somehow was doing what the Apple dox suggested, but missed a step.
3
3
2.9k
Jun ’24
How to properly destroy a child UIViewController?
I have my mainController (parent) and my menuController (child). I call the menuController with addChild(child) view.addSubview(child.view) child.didMove(toParent: self) The child dismisses itself with: self.dismiss(animated: true, completion: nil) The question I have, is how do I clean up the child within the parent? Surely I have to do something?
1
1
5.1k
Dec ’21