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
}
}
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
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)
}
Using Xcode 15.0.1
The playground, for the code below, has been in "Launching ok" for over two minutes. Anything that I should look into? A little mind boggling actually.
import UIKit
var msg = "Hello World"
print ("\(msg)")
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)
}
}
}
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?
Topic:
Developer Tools & Services
SubTopic:
Xcode
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?
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?
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
So like the title says, when I start up Xcode the preview won;t work till I run a debug session using the simulator.
Sometimes the debug session is unable to start the simulator, which I can start manually then run a debug session.
Once all the above is done, preview works.
Any idea what is causing this behavior?
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)
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?
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
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.
It's a small thing, but every time I open my current Xcode project the default iOS device to run my app on is my phone. Is there a way to change it to one of the simulators? i.e. iPhone Pro 11?
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?