Post

Replies

Boosts

Views

Activity

Location Services
With Xcode 13.2, every time I rebuild my Mac application, the first time I run it, it asks for permission to use my current location (the app does use that). With Xcode 13.1, it only asked the first time I ran the app, not every time I rebuilt, now it seems like it thinks this is a new app every time I rebuild it. I looked in Privacy settings and the app is only listed once. Is my Mac filling up with permissions to use my location for each build of my app?
0
0
300
Dec ’21
self is used before all properties are initialized (Swift)
struct ModelDemo {     let callback : () -> () } final class ViewModelDemo {     let modelDemo: ModelDemo     init() {         modelDemo = ModelDemo(callback: self.modelCallback)     }          private func modelCallback() {     } } The above generates the error "'self' used before all stored properties are initialized." I understand the error, but this is a common programming pattern for me in other languages. Is there a preferred way to do something like this in Swift? My current workaround is to make the callback property of ModelDemo into an optional and initialize it to nil, then to set it from the ViewModelDemo init() after it (and other ViewModel properties) have been initialized. FWIW, the intent of the code is to give the Model a way to inform the ViewModel that something in the Model has changed. Since the Model is supposed to be isolated from the View, I don't think I should use @ObservableObject as that's a SwiftUI feature.
4
0
3.6k
Nov ’21
Swift switch statement optimization
When a Swift program is executing a switch statement does it look at each case in turn to see which one to execute? Would it make sense to put the case's more likely to be chosen closer to the top of the statement? With a switch statement on an enum with a large (over 100) number of cases would it make sense to replace the switch with a dictionary of closures?
2
0
347
Nov ’21
CPU Usage Mapkit
I wrote a SwiftUI app for the Mac that uses MapKit to display a map for a given address. It uses geocoding to convert a street address to latitude and longitude. I place a pin on the map at the actual latitude and longitude. I have noticed that the pin appears to move slightly back and forth or up and down while it is displayed. The program isn't doing any other calculations. I have noticed that if the map is not displayed, the Mac Activity Monitor shows that the application is not using any CPU resources, but when the map is displayed, Activity Monitor shows around 10%. I'm not sure what that 10% means because Activity Monitory says that the User is only using 1.5 - 2%. What is the CPU doing? Is it waiting for the user to move or zoom the map? Is there a way to disable that and have just a static map display?
0
0
407
Nov ’21
403 Error with URLSession.downloadTask (SwiftUI)
I wrote a program that can download an updated data file publicly available from the US Government's FCC website. The file is updated weekly. This week, I went to download the file with my program and it failed to download. I looked at the HTTPURLResponse parameter to the completion callback and it shows a statusCode of 403 (forbidden). The program is the same program as last week when it worked. I copied the exact URL that the program was trying to download to both Safari and the command line curl and both were able to download the file without issue on the same computer. I changed the program to try and download a different file from a different server and it downloaded fine. I tried running the same program on a different Mac and it failed also. I tried several times for over an hour with the same result. Browsing the web for a solution I see mention of clearing caches. How does one clear the caches for a SwiftUI program? Thanks, Mark
2
0
1.2k
Nov ’21
Compile for different macOS versions
I am learning Swift/SwiftUI and working on my first substantial (for me) macOS application. Since I was following current tutorials and WWDC videos, I ended up using some features that are only available in OSX 12. Is there a way to maintain one code base that can be compiled for different target versions? I think I'm looking for something like C/C++'s #ifdef that would include different pieces of code depending on the target OS version. Mark
2
0
1k
Nov ’21
Maps / Geocoding in SwiftUI
I have written a program in SwiftUI to display information from a database that includes the street address, but not the latitude and longitude. I've added a button that passes the street address to geocodeAddressString to get the lat & long and updates the map, but every time I click the button, even though the map appears to be displayed properly, I get a message in Xcode complaining "Modifying state during view update..." My view has a @State variable that holds a MKCoordinateRegion() for the map to display. The closure on completion of the geocode lookup updates that @State.     @State private var mapRegion = MKCoordinateRegion()          private func updateAddress() {         let index = data.zipCode.index(data.zipCode.startIndex, offsetBy: 5)         geocoder.geocodeAddressString("\(data.streetAddress) \(data.zipCode[..<index])") { result, error in             DispatchQueue.main.async {                 if let coordinate = result?.first?.location?.coordinate {                     mapRegion = MKCoordinateRegion (                         center: coordinate,                         latitudinalMeters: 750,                         longitudinalMeters: 750                     )                     place = IdentifiablePlace(lat: coordinate.latitude, long: coordinate.longitude)                     // print ("updating address")                 }             }         }     }
0
0
848
Nov ’21
Xcode Simulator Rotate
I'm developing an iPad app in Xcode 13.1. I'm using the iPad Air 2 simulator (that's the physical iPad I have). I've set the simulator to be in landscape mode with the home button to the right. When my app loads, it always loads in portrait mode. If I rotate the simulator to portrait and back to landscape, the app screen rotates as desired. Why isn't the app screen properly rotated when the app first launches in the simulator? Thanks, Mark
2
1
3.5k
Oct ’21
Xcode Schemes?
I'm developing an iOS app in Xcode 13.1. I set the scheme (I think that's what says "iPad Air 2" in the screenshot) to use a given simulator, but it I exit Xcode and restart, it sometimes changes to another simulator. Is there a way to get Xcode to remember the setting? Thanks, Mark
0
0
384
Oct ’21
protocol ViewModifier / typealias Content
The definition of the protocol ViewModifer (SwiftUI.swift) includes the line "typealias Content." From what I read in the Swift docs, typealias requires a type. In fact, if I try to create a similar protocol by pasting the protocol code from SwuifUI.swift into my own file, Xcode gives the error "Type alias is missing an assigned type; use 'associatedtype' to define an associated type requirement." FWIW, the ViewModifier protocol also has the line "associatedtype Body: View" Can someone explain?
0
0
448
Oct ’21
Xcode Build Alerts
Back in August, I started trying to learn Mac programming with the Xcode 12 from the App Store as Xcode 13 was still in beta. Apparently, once 13 was out of beta, Apple automatically upgraded me. It really throws a curve in your learning process when the tools change out from under you, but I have survived. Xcode 12 had a feature where an image popped up to indicate success or failure of the build. See above. I can't reproduce this in Xcode 13. I can set up a system notification, but that's not the same.
2
0
1.5k
Oct ’21
XCODE 13 Brace matching / Code Collapsing
In XCODE 13, if I select a brace (parenthesis, bracket or brace), the matching brace briefly highlights. This is fine if the matching brace is on the screen, but the highlighting goes away before I can scroll to a matching brace if it is off the screen. Is there a way to find a matching brace that is many lines away? Is there an automatic was to highlight all the code between matching braces? Secondly, does XCODE have the capability of collapsing code between braces? Thanks, Mark
1
0
1.6k
Oct ’21
SWiftUI Inspector unavailable in XCODE Preview Canvas
I am developing a Mac app using SwiftUI and XCODE 13. I have the preview displaying, and while the SwiftUI Inspector is available when I command click in the source code, it is not available in the preview pane. The icon for the inspector in the menu above the preview is grayed out as is the icon to duplicate and the inspector icon at the bottom of the window. These icons are present in a similar app from which I adapted the views. Mark
1
0
973
Oct ’21
XCODE SwiftUI Preview
I'm trying to preview the SwiftUI screens from my Mac app on the canvas in XCODE 13. In live operation, the model on my app connects to an external device via TCP over the network. I have a an initializer (init()) for my model that initializes the model without network access and I use this in the PreviewProvider in my view file. However, the preview fails to load unless the external device is available on the network. The preview says it is unable to connect to the main application, which makes sense as the main app won't run without the device present. Why does XCODE have to run the app? and if so, what is the purpose of the PreviewProvider in the view file?
2
1
1.6k
Oct ’21