Post

Replies

Boosts

Views

Activity

How to use URLSession
I'm trying to learn SwiftUI by converting a shell script to a SwiftUI app. It involves parsing some XML data that comes from the web. I managed to get the parsing to work using sample data, but when I tried to add the final piece, actually grabbing the data from the web, I got stuck. I found some sample code that works by itself, but when I tried to put it into a function that would take a URL and return the data, it returns an empty string. I guess it's an async problem, but I can't find sample code that will work for me. This is an app that only I will use. Here is what I have. func doHttpCall(st : String) -> String { var xmlData = "" let url = URL(string: st)! let task = URLSession.shared.dataTask(with: url) {(data, response, error) in guard let data = data else { return } xmlData = String(data: data, encoding: .utf8)! print(xmlData) } task.resume() return xmlData } It does fetch the data, as it is printing it, but it's returning an empty string. Any help will be appreciated.
4
0
1.8k
Jun ’23
Selecting Multiple Items and then Sorting
I'm trying to learn Swift and SwiftUI, so I tried to write what I thought would be a simple app, but I’ve quickly run into a problem. I want to be able to select multiple languages and then be able to sort those languages. I found sample code on the web for both tasks, and I thought I understood how they worked, however they use different data structures and I can’t figure out how to reconcile them. In the code below, preferredLanguages is used to select multiple languages, and sortedLangs is used for sorting. Individually those tasks work fine, but I can’t figure out how to share the results of the multiple selection with the sorting view. Ideally I’d prefer to have just one class or struct, one var, and one view, but if I can’t do that I need to pass the preferredLanguages data to the sortedLangs var. Suggestions? Is there some sample code somewhere that does this? enum LanguageCodes: Int, CaseIterable, Identifiable { case eng = 0 case ell case deu //etc.     var id: LanguageCodes {         self     }     var literal: String {         switch self { case .eng: return "eng" case .ell: return "ell" case .deu: return "deu"         }     } } class PreferredLanguages: ObservableObject {     @Published var languages = [LanguageCodes]() } struct IdentifiableLanguageItem: Identifiable  {     let id = UUID()     let langCode: LanguageCodes } @ObservedObject var preferredLanguages = PreferredLanguages() @State private var sortedLangs: [IdentifiableLanguageItem]
4
0
1.3k
Oct ’22
File does not exist error - but it does
I'm a hobbyist and I'm trying to convert a shell script into a compiled MacOS SwiftUI app. It requires running a third party utility that I placed in /usr/local/bin. I found example code on the web that runs the /usr/bin/say command. It works just fine, but when I modify it to try to run the utility in /usr/local/bin, I receive a file does not exist error. Assuming it was some sort of permissions problem, I modified the permissions on /usr/local/bin to 777, and still get the error. I searched in the Mac's Security and Privacy and added Xcode to the Developer Tools to allow it run run software that does not meet the system's security policy. Nope. I've even copied the /usr/bin/say command over to /usr/bin/local and get the same error. I get the error on both the latest Big Sur Xcode and on the latest beta Monterey and Xcode beta. Could someone please point me the docs that explain what is happening here? Thanks. Here's the error that I'm getting: SayThis/ContentView.swift:38: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=4 "The file “say” doesn’t exist." UserInfo={NSFilePath=/usr/local/bin/say} (lldb)
1
0
3.0k
Jul ’21