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)")
Post
Replies
Boosts
Views
Activity
I hope this isn't too off-topic, but I'm in a bind
My HD is so full that I can't download any new iOS' in Xcode. Actually, there's a lot I can't do. According to the storage part of System Details, Developer is over 100 GB.
I have a huge external HD and would like to completely remove and clean Xcode, and the reinstall it on the external HD.
1 - Is this allowed? Some people say that all apps must be installed in the Applications folder. Checking the web I get both answers.
2 - If it is allowed, is there a way to properly clean out? Because every website I found that describes this procedure is touting their own cleanup app.
Thank you
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)
}
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
}
}
How can I keep a class declaration within SwiftUI view persistent through any of the view's updates?
I have a class, MyURL, from a UIKit app that I wrote that handles all my URL needs, uploads, downloads, GETS, POSTS, etc.
I would like to use that class from within a SwiftUI view now. So the SwiftUI view that creates calls MyURL methods. And I would pass binding vars that would cause my SwiftUI view to update things such as statuses : percentage done, etc.
My issue is, how can I properly declare that class, myURL, from within the SwiftUI view, so that it doesn't get redeclared every time the view updates.
Should I declare the MyURL class in a view above it and pass it into the view that calls the MyUrl class? Would just like to do it the proper way.
I wish I was better with terminology.
Thank you
struct ContentView: View {
**var myURL = MyURL()**
@State var proceed = false
var body: some View {
Button {
myURL.pressed(proceed: $proceed)
}
label: {
Text(proceed ? "pressed" : "not pressed")
}
}
}
class MyURL: NSObject, URLSessionDataDelegate,URLSessionTaskDelegate, URLSessionDelegate, URLSessionDownloadDelegate{
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
}
func pressed(proceed: Binding<Bool>) {
print("\(proceed)")
proceed.wrappedValue.toggle()
}
// Error received
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if let err = error {
DispatchQueue.main.async { [self] in
//code
}
}
}
// Response received
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: (URLSession.ResponseDisposition) -> Void) {
completionHandler(URLSession.ResponseDisposition.allow)
if let httpResponse = response as? HTTPURLResponse {
xFile?.httpResponse = httpResponse.statusCode
DispatchQueue.main.async { [self] in
if httpResponse.statusCode != 200 {
//code
}
}
}
}
// Data received
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
if xFile?.httpResponse == 200 {
DispatchQueue.main.async { [self] in
//code
}
} //DispatchQueue.main.async
}
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
let num : Float = Float(totalBytesWritten * 100)
let den : Float = Float(totalBytesExpectedToWrite * 100)
let percentDownloaded : Int = Int(num/den * 100)
DispatchQueue.main.async { [self] in
//code
}
}
}
Trying to create an app in SwiftUI that uses HTTP for "gets", "posts", "forms", et al.
I cannot assign URLSessionDataDelegate,URLSessionTaskDelegate, URLSessionDelegate to a SwiftUI view. So my assumption is that I need to create a UIViewControllerRepresentable for a regular UIViewController and assign the delegates to that UIViewController
I just need to know if this is the correct path to proceed with? Or is there some sort ion tie in that I cannot find using Google?
Thank you
Am going through a SwiftUI course, so the code is not my own.
When I migrated my @Bindings into @Published items in an @ObservableObject I started getting the following error:
Publishing changes from within view updates is not allowed, this will cause undefined behavior.
The warning occurs in the ScannerView which is integrated with the main view, BarcodeScannerView. It occurs when an error occurs, and scannerView.alertItem is set to a value.
However, it does not occur when I am setting the value of scannerView.scannedCode, and as far as I can tell, they both come from the sample place, and are the same actions.
There are tons of posts like mine, but I have yet to find an answer. Any thoughts or comments would be very appreciated.
BarcodeScannerView
import SwiftUI
struct BarcodeScannerView: View {
@StateObject var viewModel = BarcodeScannerViewModel()
var body: some View {
NavigationStack {
VStack {
ScannerView(scannedCode: $viewModel.scannedCode, typeScanned: $viewModel.typeScanned, alertItem: $viewModel.alertItem)
.frame(maxWidth: .infinity, maxHeight: 300)
Spacer().frame(height: 60)
BarcodeView(statusText: viewModel.typeScanned)
TextView(statusText: viewModel.statusText, statusTextColor: viewModel.statusTextColor)
}
.navigationTitle("Barcode Scanner")
.alert(item: $viewModel.alertItem) { alertItem in
Alert(title: Text(alertItem.title), message: Text(alertItem.message), dismissButton: alertItem.dismissButton)
}
}
}
}
BarcodeScannerViewModel
import SwiftUI
final class BarcodeScannerViewModel: ObservableObject {
@Published var scannedCode = ""
@Published var typeScanned = "Scanned Barcode"
@Published var alertItem: AlertItem?
var statusText: String {
return scannedCode.isEmpty ? "Not Yet scanned" : scannedCode
}
var statusTextColor: Color {
scannedCode.isEmpty ? .red : .green
}
}
ScannerView
import SwiftUI
struct ScannerView: UIViewControllerRepresentable {
typealias UIViewControllerType = ScannerVC
@Binding var scannedCode : String
@Binding var typeScanned : String
@Binding var alertItem: AlertItem?
func makeCoordinator() -> Coordinator {
Coordinator(scannerView: self)
}
func makeUIViewController(context: Context) -> ScannerVC {
ScannerVC(scannerDelegate: context.coordinator)
}
func updateUIViewController(_ uiViewController: ScannerVC, context: Context) {
}
final class Coordinator: NSObject, ScannerVCDelegate {
private let scannerView: ScannerView
init(scannerView: ScannerView) {
self.scannerView = scannerView
}
func didFind(barcode: String, typeScanned: String) {
scannerView.scannedCode = barcode
scannerView.typeScanned = typeScanned
print (barcode)
}
func didSurface(error: CameraError) {
switch error {
case .invalidDeviceinput:
scannerView.alertItem = AlertContext.invalidDeviceInput
case .invalidScannedValue:
scannerView.alertItem = AlertContext.invalidScannedValue
case .invalidPreviewLayer:
scannerView.alertItem = AlertContext.invalidPreviewLayer
case .invalidStringObject:
scannerView.alertItem = AlertContext.invalidStringObject
}
}
}
}
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
I am looping through an audio file, below is my very simple code.
Am looping through 400 frames each time, but I picked 400 here as a random number.
I would prefer to read in by time instead. Let's say a quarter of second. So I was wondering how can I determine the time length of each frame in the audio file?
I am assuming that determining this might differ based on audio formats? I know almost nothing about audio.
var myAudioBuffer = AVAudioPCMBuffer(pcmFormat: input.processingFormat, frameCapacity: 400)!
guard var buffer = AVAudioPCMBuffer(pcmFormat: input.processingFormat, frameCapacity: AVAudioFrameCount(input.length)) else {
return nil
}
var myAudioBuffer = AVAudioPCMBuffer(pcmFormat: input.processingFormat, frameCapacity: 400)!
while (input.framePosition < input.length - 1 ) {
let fcIndex = ( input.length - input.framePosition > 400) ? 400 : input.length - input.framePosition
try? input.read(into: myAudioBuffer, frameCount: AVAudioFrameCount(fcIndex))
let volUme = getVolume(from: myAudioBuffer, bufferSize: myAudioBuffer.frameLength)
...manipulation code
}
I would like to open an audio file on my iOS device and remove long silences. I already have the code for calculating volumes so am not pasting that here.
What I am unsure of "how to do" is: While I believe that I have the proper code to read the file below, I am unsure as to how to read it in proper pieces to I can later get the volume of each piece.
I realize that this might be a situation of calculating the size of frames and whatnot. But I am totally green when it comes to audio.
I would seriously appreciate any guidance.
guard let input = try? AVAudioFile(forReading: url) else {
return nil
}
guard let buffer = AVAudioPCMBuffer(pcmFormat: input.processingFormat, frameCapacity: AVAudioFrameCount(input.length)) else {
return nil
}
do {
try input.read(into: buffer)
} catch {
return nil
}
Updated info below
Full disclosure: I do have this question over on StackOverflow, but I am at a standstill till I find a way to move forward, debug, etc.
I am trying to recognize prerecorded speech in Swift. Essentially it either detects no speech, detects blank speech, or works on the one prerecorded file where I screamed a few words.
I can't tell where the headache lies and can't figure out if there's a more detailed way to debug this. I can't find any properties that give more detailed info.
Someone on SO did recommend I go through Apple's demo, here. This works just fine, and my code is very similar to it. Yet the main difference remains if there is something about the way I save my audio files or something else is leading to my headaches.
If anyone has any insight into this I would very much appreciate any hints.
My question over on StackOverflow
Updated info below, and new code
Updated info It appears that I was calling SFSpeechURLRecognitionRequest too often, and before I completed the first request. Perhaps I need to create a new instance of SFSpeechRecognizer? Unsure.
Regardless, I quickly/sloppily adjusted the code to only run it once the previous instance returned its results.
The results were much better, except one audio file still came up as no results. Not an error, just no text.
This file is the same as the previous file, in that I took an audio recording and split it in two. So the formats and volumes are the same.
So I still need a better way to debug this, to find out what it going wrong with that file.
The code where I grab the file and attempt to read it
func findAudioFiles(){
let fm = FileManager.default
var aFiles : URL
print ("\(urlPath)")
do {
let items = try fm.contentsOfDirectory(atPath: documentsPath)
let filteredInterestArray1 = items.filter({$0.hasSuffix(".m4a")})
let filteredInterestArray2 = filteredInterestArray1.filter({$0.contains("SS-X-")})
let sortedItems = filteredInterestArray2.sorted()
for item in sortedItems {
audioFiles.append(item)
}
NotificationCenter.default.post(name: Notification.Name("goAndRead"), object: nil, userInfo: myDic)
} catch {
print ("\(error)")
}
}
@objc func goAndRead(){
audioIndex += 1
if audioIndex != audioFiles.count {
let fileURL = NSURL.fileURL(withPath: documentsPath + "/" + audioFiles[audioIndex], isDirectory: false)
transcribeAudio(url: fileURL, item: audioFiles[audioIndex])
}
}
func requestTranscribePermissions() {
SFSpeechRecognizer.requestAuthorization { [unowned self] authStatus in
DispatchQueue.main.async {
if authStatus == .authorized {
print("Good to go!")
} else {
print("Transcription permission was declined.")
}
}
}
}
func transcribeAudio(url: URL, item: String) {
guard let recognizer = SFSpeechRecognizer(locale: Locale(identifier: "en-US")) else {return}
let request = SFSpeechURLRecognitionRequest(url: url)
if !recognizer.supportsOnDeviceRecognition { print ("offline not available") ; return }
if !recognizer.isAvailable { print ("not available") ; return }
request.requiresOnDeviceRecognition = true
request.shouldReportPartialResults = true
recognizer.recognitionTask(with: request) {(result, error) in
guard let result = result else {
print("\(item) : There was an error: \(error.debugDescription)")
return
}
if result.isFinal {
print("\(item) : \(result.bestTranscription.formattedString)")
NotificationCenter.default.post(name: Notification.Name("goAndRead"), object: nil, userInfo: self.myDic)
}
}
}
Sorry, my question was idiotic, and due to my bad typing skills (not the first time).
So I am erasing it since I can't delete it
Again, sorry
I am looking for a proper tutorial on how to write let's say a messaging app.
in other words the user doesn't have to run Messaging to get messages. I would like to build that type of structured app. I realize that "push notifications" appear the way to go.
But at this point I still can't find an decent tutorial that seems to cover all the bases.
Thank you
I have a UIViewText that I have in all my UIViewControllers that carries over data from the entire app's instance.
If I put the 2 lines that scroll to the bottom in the viewDidAppear section, the text scrolls to the bottom, but you see it occur, so it's not pleasant visually.
However, if I put the same 2 lines in the viewWillAppear section (as shown below) then for some reason the UITextView starts at the top of the text.
Am I somehow doing this incorrectly?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
myStatusWin.text = db.status
let range = NSMakeRange(myStatusWin.text.count - 1, 0)
myStatusWin.scrollRangeToVisible(range)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
Within my code to fetch data from CoreData I have the following line:
let itemNoSort = NSSortDescriptor(key:"itemNo", ascending: false)
What I am not sure of however is that the above is the same as saying descending: true
Can't seem to find it in the documentation.