Post

Replies

Boosts

Views

Activity

Reply to How can I pass a function pointer in Swift?
Thanks for the answer, I will have to study up on Selectors, hate all this different terminology. For instance the syntax: action: action, for: .touchUpInside makes no sense to me. To me, it's still a call back. Will still have to find out how to pass a pointer to a function at some point I guess. Will mark you answer correct, just want to make sure you see this comment p.s. Also confused since the sample code that worked, without having the second action with the comma:
Topic: UI Frameworks SubTopic: UIKit Tags:
Oct ’21
Reply to Why does a dismissed child UIView doesn't reappear properly after its first appearance?
Experimenting around I found that if I didn't set the child's constraints (see below), then it would always work. Is there a method to remove the constraints of a UIView before deleting it? vc is the Child UIView and pc is its parent func setConstraints (vc: UIViewController, pc: UIView) { vc.view.translatesAutoresizingMaskIntoConstraints = false var constraints = [NSLayoutConstraint]() constraints.append(vc.view.leadingAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.leadingAnchor)) constraints.append(vc.view.trailingAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.trailingAnchor)) constraints.append(vc.view.bottomAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.bottomAnchor)) constraints.append(vc.view.topAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.topAnchor)) NSLayoutConstraint.activate(constraints) }
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’21
Reply to Is .installTap the equivalent of a C callback function?
This is a rewriting of my original question, once I realized I was reading the flow incorrectly. Working on a speech to text demo, which works. But I am still trying to learn the flow of Swift. While I may be calling it incorrectly, I'm looking at the closure in node.installTap as a C callback function. When the buffer is full, the code within the closure is called. From what I interpret here, every time the buffer becomes full, the closure from within the node.installTap runs. What I can't figure out is what triggers the closure within: task = speechRecognizer?.recognitionTask(with: request, resultHandler: {}) The entire demo below works, am just trying to figure out how the AVAudioEngine knows when to call that second closure. Is there some connection? func startSpeechRecognition (){ let node = audioEngine.inputNode let recordingFormat = node.outputFormat(forBus: 0) node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, _) in self.request.append(buffer) } audioEngine.prepare() do { try audioEngine.start() } catch let error { ... } guard let myRecognition = SFSpeechRecognizer() else { ... return } if !myRecognition.isAvailable { ... } task = speechRecognizer?.recognitionTask(with: request, resultHandler: { (response, error) in guard let response = response else { if error != nil { print ("\(String(describing: error.debugDescription))") } else { print ("problem in repsonse") } return } let message = response.bestTranscription.formattedString print ("\(message)") }) }
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’21
Reply to My writing the installTap buffer to an AVAudioFile seems to fail data-wise, or at the end
It's kind of kludgy but someone gave me the answer here: https://stackoverflow.com/questions/70616820/my-writing-the-installtap-buffer-to-an-avaudiofile-seems-to-fail-data-wise/70618216?noredirect=1#comment124895854_70618216 You need to let your AVAudioFile go out of scope (nil it at some point), that's how you call AVAudioFile's close() method, which presumably finishes writing out header information.
Topic: Media Technologies SubTopic: Audio Tags:
Jan ’22
Reply to How can I get my app to wait for a permission request to complete?
So I created the code below, and the weird thing was that the output read: going in... and we're out Need to ask user So essentially it blows right through the semaphore. However, if I declare the semaphore with 0, then the first semaphore.wait() is successful, and the program freezes because the userAlert permission box never pops up. What is going on here? print ("going in...") let semaphore = DispatchSemaphore(value: 1 ) DispatchQueue.global(qos: .userInitiated).async { let mediaAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: .audio) switch mediaAuthorizationStatus { case .denied: print (".denied") case .authorized: print ("authorized") case .restricted: print ("restricted") case .notDetermined: print("Need to ask user") semaphore.wait() AVCaptureDevice.requestAccess(for: .audio, completionHandler: { (granted: Bool) in if granted { semaphore.signal() } else { semaphore.signal() } }) @unknown default: print("unknown") } print ("\(semaphore.debugDescription)") } semaphore.wait() print ("and we're out")
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’22
Reply to What are the SKPhysics settings to make a ball bounce?
So to make sure I understand...from your posts and my playing around with the code, it doesn't matter where the higher restitution is? i.e. ball, edge, block And their restitutions doesn't combine, the software just used the higher one? i.e. ball.restitution = 0.8 block.restitution = 0.4 restitution used by physics engine will be 0.8
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to How can I pass a function pointer in Swift?
Thanks for the answer, I will have to study up on Selectors, hate all this different terminology. For instance the syntax: action: action, for: .touchUpInside makes no sense to me. To me, it's still a call back. Will still have to find out how to pass a pointer to a function at some point I guess. Will mark you answer correct, just want to make sure you see this comment p.s. Also confused since the sample code that worked, without having the second action with the comma:
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to How can I use a custom image for a Swift button but still control the text separately?
So I modified the code as such: //    button.setImage(scaledImage, for: .normal)     button.setBackgroundImage(scaledImage, for: .normal) And that does work. But am unsure if I have to do anything with the .setImage method
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to How can I use a custom image for a Swift button but still control the text separately?
It works now with me only setting the background image. But can I leave setIamge blank? Or do I have to set it to nil?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to Is Multiple inheritance not allowed in Swift?
Doh! Thanks. Am going to blame the laziness of autocomplete. That’s why I like to type everything. Ingrains it better.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Nov ’21
Reply to Looking for a proper Speech to Text tutorial
For anyone coming across this with the same need, I found this to be a great tutorial: https://youtu.be/SZJ8zjMGUcY (no, it's not me)
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Nov ’21
Reply to Why does a dismissed child UIView doesn't reappear properly after its first appearance?
Experimenting around I found that if I didn't set the child's constraints (see below), then it would always work. Is there a method to remove the constraints of a UIView before deleting it? vc is the Child UIView and pc is its parent func setConstraints (vc: UIViewController, pc: UIView) { vc.view.translatesAutoresizingMaskIntoConstraints = false var constraints = [NSLayoutConstraint]() constraints.append(vc.view.leadingAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.leadingAnchor)) constraints.append(vc.view.trailingAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.trailingAnchor)) constraints.append(vc.view.bottomAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.bottomAnchor)) constraints.append(vc.view.topAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.topAnchor)) NSLayoutConstraint.activate(constraints) }
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Dec ’21
Reply to Is .installTap the equivalent of a C callback function?
No, there is no error, it's the flow I am trying to understand. The code block in-between the last { } get's called multiple times , when the buffer is full I assume, and then the code continues to run everything below it as well.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Dec ’21
Reply to Is .installTap the equivalent of a C callback function?
I'm glad I asked this, but it turns out I do NOT understand the flow. How is task = speechRecognizer?.recognitionTask(with: request, resultHandler: { (response, error) in guard let response = response else { always being called multiple times? Does it have a connection to node.installTap?
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Dec ’21
Reply to Is .installTap the equivalent of a C callback function?
This is a rewriting of my original question, once I realized I was reading the flow incorrectly. Working on a speech to text demo, which works. But I am still trying to learn the flow of Swift. While I may be calling it incorrectly, I'm looking at the closure in node.installTap as a C callback function. When the buffer is full, the code within the closure is called. From what I interpret here, every time the buffer becomes full, the closure from within the node.installTap runs. What I can't figure out is what triggers the closure within: task = speechRecognizer?.recognitionTask(with: request, resultHandler: {}) The entire demo below works, am just trying to figure out how the AVAudioEngine knows when to call that second closure. Is there some connection? func startSpeechRecognition (){ let node = audioEngine.inputNode let recordingFormat = node.outputFormat(forBus: 0) node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, _) in self.request.append(buffer) } audioEngine.prepare() do { try audioEngine.start() } catch let error { ... } guard let myRecognition = SFSpeechRecognizer() else { ... return } if !myRecognition.isAvailable { ... } task = speechRecognizer?.recognitionTask(with: request, resultHandler: { (response, error) in guard let response = response else { if error != nil { print ("\(String(describing: error.debugDescription))") } else { print ("problem in repsonse") } return } let message = response.bestTranscription.formattedString print ("\(message)") }) }
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Dec ’21
Reply to My writing the installTap buffer to an AVAudioFile seems to fail data-wise, or at the end
It's kind of kludgy but someone gave me the answer here: https://stackoverflow.com/questions/70616820/my-writing-the-installtap-buffer-to-an-avaudiofile-seems-to-fail-data-wise/70618216?noredirect=1#comment124895854_70618216 You need to let your AVAudioFile go out of scope (nil it at some point), that's how you call AVAudioFile's close() method, which presumably finishes writing out header information.
Topic: Media Technologies SubTopic: Audio Tags:
Replies
Boosts
Views
Activity
Jan ’22
Reply to Can I create a class instance using DispatchQueue.global().async and have its methods run asynchronously?
I was informed that this wouldn't work. No matter where the instance of the class was generated, its methods would still run on the main queue by default.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jan ’22
Reply to How can I get my app to wait for a permission request to complete?
So I created the code below, and the weird thing was that the output read: going in... and we're out Need to ask user So essentially it blows right through the semaphore. However, if I declare the semaphore with 0, then the first semaphore.wait() is successful, and the program freezes because the userAlert permission box never pops up. What is going on here? print ("going in...") let semaphore = DispatchSemaphore(value: 1 ) DispatchQueue.global(qos: .userInitiated).async { let mediaAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: .audio) switch mediaAuthorizationStatus { case .denied: print (".denied") case .authorized: print ("authorized") case .restricted: print ("restricted") case .notDetermined: print("Need to ask user") semaphore.wait() AVCaptureDevice.requestAccess(for: .audio, completionHandler: { (granted: Bool) in if granted { semaphore.signal() } else { semaphore.signal() } }) @unknown default: print("unknown") } print ("\(semaphore.debugDescription)") } semaphore.wait() print ("and we're out")
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Feb ’22
Reply to How can i create a second UIViewController programmatically that is independent ?
Closing because again I asked a stupid question
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Mar ’22
Reply to Is anyone aware of any bugs associated with: .modalTransitionStyle = .partialCurl ?
.flipHorizontal appears stable. Any experience with that?
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Mar ’22