Post

Replies

Boosts

Views

Activity

Using DispatchQueue
I am attempting to follow this example of how to create a camera app: https://www.appcoda.com/avfoundation-swift-guide/ It describes a sequence of 4 steps, and these steps are embodied in four functions: func prepare(completionHandler: @escaping (Error?) -> Void) { func createCaptureSession() { } func configureCaptureDevices() throws { } func configureDeviceInputs() throws { } func configurePhotoOutput() throws { } DispatchQueue(label: "prepare").async { do { createCaptureSession() try configureCaptureDevices() try configureDeviceInputs() try configurePhotoOutput() } catch { DispatchQueue.main.async { completionHandler(error) } return } DispatchQueue.main.async { completionHandler(nil) } } } What is confusing me is that it appears these four steps need to be executed sequentially, but in the Dispatch Queue they are executed simultaneously because .async is used. For example farther down that webpage the functions createCaptureSession(), and configureCaptureDevices(), and the others, are defined. In createCaptureSession() the member variables self.frontCamera, and self.rearCamera, are given values which are used in configureCaptureDevices(). So configureCaptureDevices() depends on createCaptureSession() having been already executed, something that it appears cannot be depended upon if both functions are executing simultaneously in separate threads. What then is the benefit of using DispatchQueue()? How is it assured the above example dependencies are met? What is the label parameter of the DispatchQueue()'s initializer used for?
1
0
858
Jul ’23
Can't Create Storyboard Outlet
I am not able drag a reference object from a Storyboard screen to code in the Assistant. The expected blue line appears as the screenshot shows, but no code is created in the Assistant. This reference object is not in the Launch Screen. When the the screen this object is in is highlighted, the correct file opens in the Assistant. Dragging this object from the "Content View" folder also does not work, even though the blue line also appears. I was recently able to do this. Something changed. What might have changed?
1
0
1k
Jul ’23
UIButton Changing Foreground, and Background, Colors
The goal is to have the background, and foreground, colors show the current state of a UIButton on a storyboard file. The attempt to do that is done with this code: var toggleLight: Bool = false @IBAction func toggleLight(_ sender: UIButton) { if( toggleLight ){ toggleLight = false sender.baseBackgroundColor = UIColor.black sender.baseForegroundColor = UIColor.white }else{ toggleLight = true sender.baseBackgroundColor = UIColor.white sender.baseForegroundColor = UIColor.black } tableView.reloadData() } I get these errors: Value of type 'UIButton' has no member 'baseBackgroundColor' Value of type 'UIButton' has no member 'baseForegroundColor' I do not understand this because in file "UIKit.UIButton" I see: extension UIButton { ... public var baseForegroundColor: UIColor? public var baseBackgroundColor: UIColor? ... } What has gone wrong there?
0
0
711
Jun ’23
How to Set White Balance?
Here is a code snippet by which I attempted to set a neutral white balance: func prepare(completionHandler: @escaping (Error?) -> Void) { func createCaptureSession() { self.captureSession = AVCaptureSession() } func configureCaptureDevices() throws { let session = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back) let cameras = (session.devices.compactMap { $0 }) if cameras.isEmpty { throw CameraControllerError.noCamerasAvailable } for camera in cameras { try camera.lockForConfiguration() camera.setWhiteBalanceModeLocked( with whiteBalanceGains: AVCaptureDevice.WhiteBalanceGains( redGain: 1.0, greenGain: 1.0, blueGain: 1.0), completionHandler handler: ((CMTime) -> Void)? = nil ) // Errors: Extra arguments at positions #2, #3 in call, Cannot find 'with' in scope camera.whiteBalanceMode = .locked camera.unlockForConfiguration() }catch{ // To Do: Error handling here } } My call to "AVCaptureDevice.WhiteBalanceGains()" gets errors. What is the correct way to do this?
0
0
741
Jun ’23
Accessing Interface Builder GUI Controls Anywhere in the App
I used the Interface Builder to place a Switch object in a view. I ctrl dragged it into the Assistant to make its handler a method of the class the view's is in, which is itself a subclass of "UITableViewController". A dialog box appear into which I entered the function name, and select the the option to have the sender as an argument. The result is function of the form: @IBAction func switchStateChange(_ sender: UISwitch) { } Now I need to query this Switch's On/Off state from elsewhere in the program, and if necessary change its state. It is not a good solution to save the sender parameter to a global variable because then it would require the user to change this switch's state before that global variable is set. What is needed is to identify, and lookup, this switch object to access it from anywhere in the application. How is this done?
1
0
744
Jun ’23
Storyboard Boolean Switch Object
The Storyboard Interface Builder has a Switch object that toggles between On, and Off, states. While in the On state it looks normal. While in the Off state it is grayed out. I have a use for this object for the user to toggle between two options which are other than On, and Off. For my intended use its Off state gray out is undesirable. Is there way to prevent this switch graying out in its Off state?
0
0
400
Jun ’23
case keyword used without switch
In TicTacToe example there is in the override method "PasscodeViewController.viewDidLoad()" this snippet of code: if let browseResult = browseResult, case let NWEndpoint.service(name: name, type: _, domain: _, interface: _) = browseResult.endpoint { title = "Join \(name)" } What confuses me: The use of the "case" keyword without a switch statement. The "case" keyword does not have a constant to compare with to decide if will branch here. And what of the method call to NWEndpoint.service() being set equal to something? Is this actually defining what the service method will do when the system calls it?
9
0
1.2k
Jun ’23
Every added View Controller adds a Launch Screen
I am still very new to Swift. Now I am learning about storyboards. I added two View Controllers to the app by using the + icon in Xcode's Title Bar. Added one button to each of their screens, and then added a connectors (segues) between them, so that each button would navigate to the other screen. As soon as these connectors are added, each segue gets the error: /Users/ ... /LaunchScreen.storyboard Launch screens may not have triggered segues. So I figured at first that the first screen I added to the project by means of adding a View Controller must have been launch screen. So I remove the segues, I add two more screens in the same way, and make the same connections between these two new screens. The same errors appeared. It appears to me that Xcode considers every new screen added by dragging in a View Controller is a launch screen. How do I make only the first screen the "Launch Screen"?
1
0
1k
Jun ’23
Reconfirming the DNS the Record
On the 4th question of: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/NetServices/Articles/faq.html I see: "you can ask the Bonjour to reconfirm the record" I am basing my code on the TicTacToe example at: https://developer.apple.com/videos/play/wwdc2022/110339/ I believe the DNS records which remains after the device that put them there shuts down is causing problems for me when again. Using the DeviceDiscoveryUI as in that TicTacToe example how are these records "reconfirmed" ?
3
0
886
Jun ’23
Phantom Service
To learn how to do peer to peer communication I downloaded the TicTacToe example into my MacStudio from: https://developer.apple.com/documentation/network/building_a_custom_peer-to-peer_protocol I then loaded it into Xcode, compiled it, deployed to an iPhone 14 over a Lightning cable, and ran it. I developed code for the MacStudio in an attempt to communicate with it. The MacStudio at first detected the service but for some reason it has quit doing so. The command: dns-sd -B _services._dns-sd._udpoutputs this as one of its lines: A/R Flags if Domain Service Type Instance Name Add 3 17 . tcp.local. _tictactoe (The time stamp column deleted for clarity) This line remains the command's output even after the iPhone the TicTacToe app has been shutdown, and after in the MacStudio Xcode, and the simulator it opens, is shutdown. In an attempt to find out what application is still advertising this Bonjour service I installed Discovery from: https://apps.apple.com/us/app/discovery-dns-sd-browser/id305441017 When run the _tictactoe service instance does show in its list also also. But when I left click this item in that list to get a detail view of it I get a perpetual spinning wheel, and just to the right of it the message "Scanning...". No information is displayed. What does that mean in regard to what program is still advertising this service? What other ways are available to find this program so it can be shut down? Is it possible that this presence in that list, and in the output of the dns-sd is just a phantom vestige of an application that is not longer running? If this is a case how can the service offering be shutdown?
1
0
500
Jun ’23
Mystery Startup Applications
On my MacStudio there are applications that start up when on login. I need to these not to be. No applications show up at: System Settings => General => Login Items Where else is there to look? The startup apps that are opening are: iPhone 14 Pro simulator Activity Monitor I believe the simulator is opening because I have been developing an iPhone 14 app with XCode. But once started, somehow, like the Sorcerer's Apprentice's brooms, it just keeps on appearing on its own, even if I have closed both it, and XCode, before shutdown. So now, to keep this off at startup, I need a sorcerer?
0
0
318
Jun ’23