Post

Replies

Boosts

Views

Activity

Color Literal not working
Is there something I need to do to enable the use of color literals in a SwiftUI View? When I type “.fill(Color(Color Literal”…, it does not recognize it. I can get to the literal picker through the assets catalog, but not from any other method. I have tried copying code from the online demos and it doesn’t work, says it isn’t recognized. I am using XCode 5.5 and targeting ios14.
0
0
735
Nov ’21
JSON decoding challenges with Combine
I am using a Combine URLSession to pull data from the Food Data Central API and the Data I am receiving is not being decoded with the JSON decoder. I know the Data is being received because I use the String(data:encoding: .utf8) as a debug print and I can see the downloaded data correctly in the console. I get an error message after the .decode completion failure that says "The data couldn't be read because it isn't in the correct format." I am guessing I have to add something like the "encoder: utf8" statement in the .decode function. Or maybe transform the data in the .tryMap closure before returning. But I have searched the documentation and other sources and have not found anywhere that discusses this. I am a fairly new to Swift (my first real app), I am hoping someone more-experienced can point me in the right direction. My code is as follows: private func fdcSearch(searchFor searchText: String) {         let query = "https://api.nal.usda.gov/fdc/v1/foods/search?api_key=***&query=+Apple%20+Fuji"         let searchURL = "https://api.nal.usda.gov/fdc/v1/foods/search?"         let searchQuery = "&query="+searchString         print(searchURL+devData.apiKey+searchQuery) //        guard let url = URL(string: "https://api.nal.usda.gov/fdc/v1/foods/search?api_key=***&query=AppleFuji") else {         guard let url = URL(string: searchURL+devData.apiKey+searchQuery) else {             print("Guard error on url assignment") // debug statement             return         }         print("In fdcSearch") // debug statement         fdcSearchSubscription = URLSession.shared.dataTaskPublisher(for: url)             .subscribe(on: DispatchQueue.global(qos: .default))             .tryMap { (output) -> Data in                 guard let response = output.response as? HTTPURLResponse, response.statusCode >= 200 && response.statusCode < 300 else {                     print("bad server response") // debug statement                     throw URLError(.badServerResponse)                 }                 print("got output") // debug statement                 if let dataString = String(data: output.data, encoding: .utf8) { // debug statement                         print("got dataString: \n\(dataString)") // debug statement                     } // debug statement                 return output.data             }             .receive(on: DispatchQueue.main)             .decode(type: [FDCFoodItem].self, decoder: JSONDecoder())             .sink { (completion) in                 switch completion {                 case .finished:                     print("Completion finished") // debug statement                     break                 case .failure(let error):                     print("Completion failed") // debug statement                     print(error.localizedDescription)                 }             } receiveValue: { [weak self] (returnedFoods) in                 self?.foods = returnedFoods                 print("returnedFoods: \(returnedFoods)") // debug statement                 print("self?.foods: \(String(describing: self?.foods))") // debug statement             }     } Any suggestions on how to handle this?
5
0
2.4k
Oct ’21
Color Literal not working
Is there something I need to do to enable the use of color literals in a SwiftUI View? When I type “.fill(Color(Color Literal”…, it does not recognize it. I can get to the literal picker through the assets catalog, but not from any other method. I have tried copying code from the online demos and it doesn’t work, says it isn’t recognized. I am using XCode 5.5 and targeting ios14.
Replies
0
Boosts
0
Views
735
Activity
Nov ’21
JSON decoding challenges with Combine
I am using a Combine URLSession to pull data from the Food Data Central API and the Data I am receiving is not being decoded with the JSON decoder. I know the Data is being received because I use the String(data:encoding: .utf8) as a debug print and I can see the downloaded data correctly in the console. I get an error message after the .decode completion failure that says "The data couldn't be read because it isn't in the correct format." I am guessing I have to add something like the "encoder: utf8" statement in the .decode function. Or maybe transform the data in the .tryMap closure before returning. But I have searched the documentation and other sources and have not found anywhere that discusses this. I am a fairly new to Swift (my first real app), I am hoping someone more-experienced can point me in the right direction. My code is as follows: private func fdcSearch(searchFor searchText: String) {         let query = "https://api.nal.usda.gov/fdc/v1/foods/search?api_key=***&query=+Apple%20+Fuji"         let searchURL = "https://api.nal.usda.gov/fdc/v1/foods/search?"         let searchQuery = "&query="+searchString         print(searchURL+devData.apiKey+searchQuery) //        guard let url = URL(string: "https://api.nal.usda.gov/fdc/v1/foods/search?api_key=***&query=AppleFuji") else {         guard let url = URL(string: searchURL+devData.apiKey+searchQuery) else {             print("Guard error on url assignment") // debug statement             return         }         print("In fdcSearch") // debug statement         fdcSearchSubscription = URLSession.shared.dataTaskPublisher(for: url)             .subscribe(on: DispatchQueue.global(qos: .default))             .tryMap { (output) -> Data in                 guard let response = output.response as? HTTPURLResponse, response.statusCode >= 200 && response.statusCode < 300 else {                     print("bad server response") // debug statement                     throw URLError(.badServerResponse)                 }                 print("got output") // debug statement                 if let dataString = String(data: output.data, encoding: .utf8) { // debug statement                         print("got dataString: \n\(dataString)") // debug statement                     } // debug statement                 return output.data             }             .receive(on: DispatchQueue.main)             .decode(type: [FDCFoodItem].self, decoder: JSONDecoder())             .sink { (completion) in                 switch completion {                 case .finished:                     print("Completion finished") // debug statement                     break                 case .failure(let error):                     print("Completion failed") // debug statement                     print(error.localizedDescription)                 }             } receiveValue: { [weak self] (returnedFoods) in                 self?.foods = returnedFoods                 print("returnedFoods: \(returnedFoods)") // debug statement                 print("self?.foods: \(String(describing: self?.foods))") // debug statement             }     } Any suggestions on how to handle this?
Replies
5
Boosts
0
Views
2.4k
Activity
Oct ’21