Post

Replies

Boosts

Views

Activity

How to use a fork of a package in Xcode project?
There is a package that I want to use in my iOS app project. Although it lacked certain functionality, the GitHub community is great and it was added by some kind soul. However, as it was not yet merged with original repository, this functionality lives only in the fork. And that's when there came the trouble. Although I'm supplying the Package Manager with the link to the fork, it does a great job in correcting me and points me to the original repo. I've seen that one can lock onto a specific commit hash in this article: https://www.swiftbysundell.com/articles/managing-dependencies-using-the-swift-package-manager/#adding-remote-dependencies, but it was only shown for Packages, not Projects in Xcode. Is there a way out of it? Is there a file I could edit to bring me the functionality I crave before I grow old?
1
1
1k
Jul ’22
How to properly create a URL with parameters given by a custom type?
TL;DR - the API cannot be changed, and the variable parameters are a vital part of my app. I've created a type that stores parameters for an API call. To make use of them with a successful request, I first need to format them into the URL. Say, my parameters are expressed as such: struct Parameters: Encodable { var name: String var taste: Taste var numberOfSpoons: Int? var topping: Taste? private enum CodingKeys: String, CodingKey { case name, taste, topping case numberOfSpoons = "spoons" } init(name: String, taste: Taste, numberOfSpoons: Int? = 1, topping: Taste? = nil){ self.name = name self.taste = taste self.numberOfSpoons = numberOfSpoons self.topping = topping } } Notice that my structure uses both Optionals (as the API does not need all the parameters from my app) and CodingKeys (as the names requested by the API look rather ugly in the code, but are necessary). Here's the Taste - not much going on except explicit Encodable conformance. enum Taste: String, Encodable { case choco, strawberry, vanilla } Now I want to call the API, so, according to the suggestion of John Sundell (I can't provide a link to the article due to Apple's censorship), I'm using URLComponents. For example: var components = URLComponents() components.scheme = "ftp" components.host = "icecream.ogs" components.path = "/spoons" If I were to add queryItems by hand, it would be fairly easy. components.queryItems = [ URLQueryItem(name: "name", value: iceCreamParameters.name), URLQueryItem(name: "numberOfSpoons", value: "\(iceCreamParameters.numberOfSpoons!)") ] How to dynamically create URLQueryItems from a non-CaseIterable type? The URLQueryItem needs both name and value parameters. I want the i-th name to be equal to I-thParameters.PropertyName, the same goes for values. As CaseIterable would not be feasible to implement* in the Parameters struct, I tried to fake it using the Encodable protocol - I thought about serializing the type to JSON (as the encoder gets rid of nil values, too) and then somehow get the keys from a deserialized Dictionary of AnyType (?), but there's got to be a better way. If this approach is not feasible, please provide your answers with helpful examples.
2
0
2.9k
Jun ’22
How to change and use a State variable with Picker?
I seem to struggle with Picker. Having read the documentation, I still feel that I'm missing something. In my project, I want to be able to switch between tabs of content. As the tabs are mutually exclusive, the Picker seems as the best choice. Say, my Tab Is declared as such: struct Tab: Identifiable, Hashable { var id: UUID var name: String } Then, as reprex, let's say I want to display the name of the tab I've chosen. After trying to get the binding on the Tab element itself, I've found this answer on SO and tried to achieve it like this: struct Test: View {     @State private var selectedTabIndex: Int = 0     var tabs: [Tab] = ["Work", "Hobby", "Random"].map {Tab(id: UUID(), name: $0)}     var body: some View {         VStack {             Picker("Tab", selection: $selectedTabIndex) {                 ForEach(tabs) {tab in                     Text(tab.name)                 }             }             Text(tabs[selectedTabIndex].name)         }     } } This example results in the displayed name not updating. If I try to select the tab using the bounded syntax, i.e. tabs[$selectedTabIndex].name, I get type mismatch between types - Cannot convert value of type 'Binding<Int>' to expected argument type 'Int'. Which is understandable, but does not get me any closer. What should I do to get this to work? Is the index approach feasible? If so, why does the view not update? What should I read about?
2
0
562
May ’22
SwiftUI Tutorial - Scrumdinger - mysterious AVPlayer initialization syntax
I've been following the Scrumdinger tutorial and had close to no trouble understanding the concepts, supplementing myself with the Swift guide. However, in the state and lifecycle lesson, exactly in the complete project, I've found confusing syntax that I cannot decipher with certainty. private var player: AVPlayer { AVPlayer.sharedDingPlayer } What does the closure after the AVPlayer type mean? sharedDingPlayer is a static property extending AVPlayer, so my guess is that it's either some kind of casting to this exact type or assigning the static prop to the player property when it's available. I would appreciate any help in clearing this out!
2
0
1.2k
May ’22