Friday, October 7, 2022
HomeiOS Developmentios - How do I correctly name this async operate to return...

ios – How do I correctly name this async operate to return information?


I am working by Develop in Swift, within the means of decoding JSON right into a customized mannequin object. When utilizing the operate to unwrap the information I get the error “Anticipated ‘(‘ in argument checklist of operate declaration” & Anticipated ‘func’ key phrase in occasion technique declaration. I do not see any cause the code should not compile. Is that this a bug?

  struct StoreItem: Codable {
    var identify: String
    var artist: String
    var type: String
    var description: String
    var artworkURL: URL
    
    init(from decoder: Decoder) throws {
        let values = attempt decoder.container(keyedBy: CodingKeys.self)
        identify = attempt values.decode(String.self, forKey: CodingKeys.identify)
        artist = attempt values.decode(String.self, forKey: CodingKeys.artist)
        type = attempt values.decode(String.self, forKey: CodingKeys.type)
        artworkURL = attempt values.decode(URL.self, forKey: CodingKeys.artworkURL)
        
        if let description = attempt? values.decode(String.self, forKey: CodingKeys.description) {
            self.description = description
        } else {
            let additionalValues = attempt decoder.container(keyedBy: AdditionalKeys.self)
            description = (attempt? additionalValues.decode(String.self, forKey: AdditionalKeys.longdescription)) ?? ""
        }
        
    }
    
    enum CodingKeys: String, CodingKey {
        case identify = "trackName"
        case artist = "artistName"
        case type
        case description
        case artworkURL = "artworkUrl"
    }
    
    enum AdditionalKeys: CodingKey {
        case longdescription
    }
    
}

struct SearchResponse: Codable {
    let outcomes: [StoreItem]
    
    enum StoreItemError: Error, LocalizedError {
        case itemNotFound
    }
    
    func fetchItems(matching question: [String: String]) async throws -> [StoreItem] {
        var urlComponents = URLComponents(string:  "https://itunes.apple.com/search")!
        urlComponents.queryItems = question.map {
            URLQueryItem(identify: $0.key, worth: $0.worth)
        }
        let (information, response) = attempt await URLSession.shared.information(from: urlComponents.url!)
        guard let httpResponse = response as? HTTPURLResponse,
              httpResponse.statusCode == 200 else {
            throw StoreItemError.itemNotFound
        }
        let decoder = JSONDecoder()
        let searchResponse = attempt decoder.decode(SearchResponse.self, from: information)
        
        return searchResponse.outcomes
        
    }
    
    var question = [
        "term": "Apple",
        "media": "ebook",
        "attribute": "authorTerm",
        "lang": "en_us",
        "limit": "10"
    ]
    
    Activity {
        do {
            let storeItems = fetchItems(matching: question)
            storeItems.forEach { merchandise in
                print("""
Identify: (merchandise.identify)
""")
            }
        }
        catch {
            print(error)
        }
    }

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments