I'm trying to receive data from a JSON link in Swift Playground on Mac, I've struct all the data, but I'm having issue trying to decode all of the data, receiving the error: "Referencing instance method 'decode(_:from:)' on 'Array' requires that 'Bicimia' conform to 'Decodable'"
I've already tries to add the Codable/Decodable option, and tried to change the URLSession respectively, but nothing has changed.
struct Bicimia {
let network: Network
}
struct Network {
let company: [String]
let href, id: String
let location: Location
let name: String
let stations: [Station]
}
struct Location {
let city, country: String
let latitude, longitude: Double
}
struct Station {
let emptySlots: Int
let extra: Extra
let freeBikes: Int
let id: String
let latitude, longitude: Double
let name, timestamp: String
}
struct Extra {
let extraDescription: String
let status: Status
}
enum Status {
case online
}
let url = "https://api.citybik.es/v2/networks/bicimia"
let urlOBJ = URL(string: url)
URLSession.shared.dataTask(with: urlOBJ!) {(data, response, error) in
do {
let res = try JSONDecoder().decode([Bicimia].self, from: data!)
print(res)
}
catch {
print(error)
}
}.resume()
To be Decodable all properties should be Decodable down the chain:
struct Bicimia: Decodable {
let network: Network // should be decodable
}
struct Network: Decodable {
let company: [String]
let href, id: String
let location: Location // should be decodable
let name: String
let stations: [Station] // should be decodable
}
struct Location: Decodable {
let city, country: String
let latitude, longitude: Double
}
struct Station: Decodable {
let emptySlots: Int
let extra: Extra // should be decodable
let freeBikes: Int
let id: String
let latitude, longitude: Double
let name, timestamp: String
}
struct Extra: Decodable {
let extraDescription: String
let status: Status // should be decodable
}
enum Status: String, Decodable {
case online
}
Note that enums can not be Decodable alone, because they should know what is the raw value, or you should manually decode them in decode function.
Related
I have several URLs and, accordingly, there is a data structure for each of them.
URLS:
case "Get Day":
return "time/get_day.php"
case "Get Time":
return "time/get_time.php"
case "Get Current Time":
return "user/get_current_time.php"
STRUCTS:
struct Day: Codable {
var status: Int? = nil
var error_message: String? = nil
var result: [Result]? = nil
}
struct Time: Codable {
let status: Int?
let error_message: String?
let result: [Result]?
struct Result: Codable {
let id: String
let startTime: String
let endTime: String
}
}
struct CurrentTime: Codable {
let status: Int?
let error_message: String?
let current_time: Int?
}
struct Result: Codable {
let id: String
let name_en: String
let name_ru: String
let name_kk: String
}
At the moment I have a parseJson () function. In which I can manually change the type of structure for parsing one by one. But I cannot think of how to do this so that I would not change anything in the code manually.
func parseJson(data: Data) {
let decoder = JSONDecoder()
do {
let parsedData = try decoder.decode(Day.self, from: data)
print(parsedData)
} catch {
print("Error parsing Json:\(error)")
}
}
Please, if you have an example or ideas, share with me.
// Generic function to decode any decodable struct
func parseJson<T: Decodable>(data: Data) -> T? {
let decoder = JSONDecoder()
do {
let parsedData = try decoder.decode(T.self, from: data)
return parsedData
} catch {
return nil
}
}
// Usage
let someDay: Day? = parseJson(data: dayData)
let sometime: Time? = parseJson(data: timeData)
I am trying to get the value of "price" key which is "25"
I am getting this response Json From Backend
{
"errorCode": 0,
"message": "Request successfully served.",
"data": {
"games": {
"TWELVEBYTWENTYFOUR": {
"jackpot_amount": "KES 40,000.00",
"draw_date": "2021-05-21 10:59:45",
"extra": {
"jackpotAmount": 40000,
"unitCostJson": [
{
"currency": "KES",
"price": 25
}
]
},
}
},
"currentTime": {
"date": "2021-05-20 22:28:18.738038"
}
}
}
This is my code so far :
fetchData { (dict, error) in
let playerLoginInfo = dataDict["data"] as? NSDictionary
let playerGameInfo = playerLoginInfo?.value(forKey: "games") as? NSDictionary
if let TWELVEBYTWENTYFOUR = playerGameInfo?.value(forKey: "TWELVEBYTWENTYFOUR") as? NSDictionary {
let extra = TWELVEBYTWENTYFOUR.value(forKey: "extra") as? NSDictionary
let unitCostJson = extra?.value(forKey: "unitCostJson") as? NSArray
print("price")
print(unitCostJson?.value(forKey: "price") as? Any)
}
}
I get this is console :
Optional(Optional(<__NSSingleObjectArrayI 0x600001f091d0>(
25
)
))
I have seen this question How can I access values within Optional NSSingleObjectArrayI? but I couldn't figure out a solution
Edit:
I have now used Codeable to get data:
struct Resp: Codable {
let errorCode: Int
let message: String
let data: Dat
}
struct Dat: Codable {
let games: Games
let currentTime: CurrentTime
}
struct Games: Codable {
let game_code: String
let datetime: String
let estimated_jackpot: String
let guaranteed_jackpot: String
let jackpot_title: String
let jackpot_amount: String
let draw_date: String
let extra: Extra
let next_draw_date: String
let active: String
}
struct Extra: Codable {
let currentDrawNumber: Int
let currentDrawFreezeDate: String
let currentDrawStopTime: String
let jackpotAmount: Int
let unitCostJson: [UnitCostJson]
}
struct UnitCostJson: Codable {
let currency: String
let price: Int
}
struct CurrentTime: Codable {
let date: String
let timezone_type: Int
let timezone: String
}
I'm trying to get value from price now with this code
do{
let resp:Resp = try JSONDecoder().decode(Resp.self , from:data);
let data = resp.data
let games = data.games
let extra = games.extra
let unitCostJson = extra.unitCostJson
print(unitCostJson[0].price)
}
catch{
GlobalFunctions.shared.callOnMainThread {
self.showAlert(Message: "Something went wrong. Please retry.")
}
}
It is going into catch
How should I get the data inside on the unitCostJson now??
I butchered your struct and removed any irrelevant properties (compared to the json), if you want to add them back then you need to use an CodingKey enum
struct Resp: Codable {
let errorCode: Int
let message: String
let data: Dat
}
struct Dat: Codable {
let games: [String:Games]
let currentTime: CurrentTime
}
struct Games: Codable {
let extra: Extra
}
struct Extra: Codable {
let unitCostJson: [UnitCostJson]
}
struct UnitCostJson: Codable {
let currency: String
let price: Int
}
struct CurrentTime: Codable {
let date: String
}
Now you can access the unitCost like this
let unitCost = resp.data.games["TWELVEBYTWENTYFOUR"]?.extra.unitCostJson
I'm building a Swift app and testing in an Xcode Playground. Calling the NYTimes Search API and trying to store its response in a struct. The code executes cleanly and no errors appear (I am using a do, try, catch), but I cannot print any properties from the resulting object (print(json.status)).
My hunch is that something is fishy with this line but I'm not sure what since no errors are printing from the catch statement
let task = URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: { data, response, error in
Form the URL endpoint to make the API call:
func APICall() {
let APIKey = "MY_API_KEY_GOES_HERE_BUT_IT'S_A_SECRET"
let searchTerm = "A Super Bowl Sideshow: See the Ageless Man!"
// Remove the spaces and convert them to percents
guard let encodedSearchTerm = searchTerm.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
else {
print("Error encoding search term in URL")
return
}
let url = "https://api.nytimes.com/svc/search/v2/articlesearch.json?q=" + encodedSearchTerm + "&api-key=" + APIKey
getData(from: url)
}
Data Task:
func getData(from url: String) {
//I believe something is wrong with the following line but I'm not sure what it is
let task = URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: { data, response, error in
guard let data = data, error == nil else {
print("Error loading data")
return
}
var result: NYTSearchResponse?
do {
result = try JSONDecoder().decode(NYTSearchResponse.self, from: data)
} catch {
print(error)
}
guard let json = result else {
print("Error assigning result to json")
return
}
//Try to print these from the resulting object but these commands do not print
print(json.status)
print(json.response.docs[0].abstract)
})
task.resume()
}
My NYTSearchResponse struct which mirrors the NYT API JSON response. It's pretty complicated, but I pasted the json response into https://app.quicktype.io/ to build the struct.
// MARK: - Welcome
struct NYTSearchResponse: Codable {
let status, copyright: String
let response: Response
}
// MARK: - Response
struct Response: Codable {
let docs: [Doc]
let meta: Meta
}
// MARK: - Doc
struct Doc: Codable {
let abstract: String
let webURL: String
let snippet, leadParagraph, printSection, printPage: String
let source: String
let multimedia: [Multimedia]
let headline: Headline
let keywords: [Keyword]
let pubDate: Date
let documentType, newsDesk, sectionName, subsectionName: String
let byline: Byline
let typeOfMaterial, id: String
let wordCount: Int
let uri: String
enum CodingKeys: String, CodingKey {
case abstract
case webURL = "web_url"
case snippet
case leadParagraph = "lead_paragraph"
case printSection = "print_section"
case printPage = "print_page"
case source, multimedia, headline, keywords
case pubDate = "pub_date"
case documentType = "document_type"
case newsDesk = "news_desk"
case sectionName = "section_name"
case subsectionName = "subsection_name"
case byline
case typeOfMaterial = "type_of_material"
case id = "_id"
case wordCount = "word_count"
case uri
}
}
// MARK: - Byline
struct Byline: Codable {
let original: String
let person: [Person]
let organization: String?
}
// MARK: - Person
struct Person: Codable {
let firstname: String
let middlename: String?
let lastname: String
let qualifier, title: String?
let role, organization: String
let rank: Int
}
// MARK: - Headline
struct Headline: Codable {
let main: String
let kicker, contentKicker: String?
let printHeadline: String
let name, seo, sub: String?
enum CodingKeys: String, CodingKey {
case main, kicker
case contentKicker = "content_kicker"
case printHeadline = "print_headline"
case name, seo, sub
}
}
// MARK: - Keyword
struct Keyword: Codable {
let name, value: String
let rank: Int
let major: String
}
// MARK: - Multimedia
struct Multimedia: Codable {
let rank: Int
let subtype: String
let caption, credit: String?
let type, url: String
let height, width: Int
let legacy: Legacy
let subType, cropName: String
enum CodingKeys: String, CodingKey {
case rank, subtype, caption, credit, type, url, height, width, legacy, subType
case cropName = "crop_name"
}
}
// MARK: - Legacy
struct Legacy: Codable {
let xlarge: String?
let xlargewidth, xlargeheight: Int?
}
// MARK: - Meta
struct Meta: Codable {
let hits, offset, time: Int
}
I moved the code out of the playground and it works.
I am building a small Weather app that is accessing the openweathermap API. I am using JSONDecoder to parse the JSON from the API. For the most part, I am able to get most of the data in the simulator. Except for the UIImage that is supposed to appear on the screen. The image is in the image.xcassets. Below is the struct.
import UIKit
import CoreLocation
struct WeatherData: Codable {
let coord: Coord
let weather: [Weather]
let base: String
let main: Main
let visibility: Int
let wind: Wind
let clouds: Clouds
let dt: Int
let sys: Sys
let id: Int
let name: String
let cod: Int
}
struct Clouds: Codable {
let all: Int
}
struct Coord: Codable {
let lon, lat: Double
}
struct Main: Codable {
let temp: Double
let pressure, humidity: Int
let tempMin, tempMax: Double
// enum CodingKeys: String, CodingKey {
// case temp, pressure, humidity
// case tempMin = "temp_min"
/ / case tempMax = "temp_max"
// }
}
struct Sys: Codable {
let type, id: Int
let message: Double
let country: String
let sunrise, sunset: Int
}
struct Weather: Codable {
let id: Int
let main, description, icon: String
}
struct Wind: Codable {
let speed: Double
let deg: Int
}
The code that accesses that passes the JSON is below:
private func getWeatherData(parameters: [String : String]) {
guard let lat = parameters["lat"],
let long = parameters["long"],
let appID = parameters["appid"] else { print("Invalid parameters"); return }
var urlComponents = URLComponents(string: "https://api.openweathermap.org/data/2.5/weather")!
let queryItems = [URLQueryItem(name: "lat", value: lat),
URLQueryItem(name: "lon", value: long),
URLQueryItem(name: "appid", value: appID)]
urlComponents.queryItems = queryItems
guard let url = urlComponents.url else { return }
URLSession.shared.dataTask(with: url) { ( data, response, err ) in
DispatchQueue.main.async { // never, never, never sync !!
if let err = err {
print("Failed to get data from url:", err)
return
}
guard let data = data else { return }
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let city = try decoder.decode(WeatherData.self, from: data)
print(city)
//self.updateWeatherData(city)
self.weatherData.description = city.weather[0].main
self.weatherData.temperature = Int(city.main.temp - 273)
self.weatherData.city = city.name
self.weatherData.condition = city.weather[0].id
WeatherDataModel().updateWeatherIcon(condition: self.weatherData.condition)
self.updateUIWeatherData()
} catch {
print(error)
self.cityLabel.text = "Connection issues"
}
}
}.resume()
}
and the function that show the data on the simulator is:
func updateUIWeatherData() {
cityLabel.text = weatherData.city
temperatureLabel.text = String(weatherData.temperature)
decriptionLabel.text = String(weatherData.description)
weatherIcon.image = UIImage(named: weatherData.weatherIconName)
}
I have looked at other example of this waring and not really sure what this means in reference to this app.
Example of result of call is unused.
Any help would be appreciated.
It seems to me like you want the line
self.weatherData.weatherIconName = WeatherDataModel().updateWeatherIcon(condition: self.weatherData.condition)
instead of
WeatherDataModel().updateWeatherIcon(condition: self.weatherData.condition)
The warning is saying you're calculating the weatherIcon, but it's not being assigned to anything (your weatherData variable)
I'm trying to make an iOS app that uses the OpenWeatherMap API to check the current weather, but I'm getting an error saying 'Type 'MyWeather' does not conform to protocol 'Encodable''. I am new to Swift Programming and it's probably a simple mistake. I would appreciate any help, thank you.
My code below:
struct MyWeather: Codable {
let name: String?
let location: String?
let temp: URL?
let wind: Int?
//THE NAMES OF THE JSON STUFF IN THE LINK
private enum CodingKeys: String, CodingKey {
case weather
case name
case location
case temp
case wind
//THE NAMES OF THE JSON STUFF IN THE LINK
}
}
class ViewController: UIViewController {
#IBAction func ShowWeatherInfo(_ sender: Any) {
guard let APIUrl = URL(string: "http://api.openweathermap.org/data/2.5/weather?q=Crowland&appid=APIKEY&units=Metric") else { return }
URLSession.shared.dataTask(with: APIUrl) { (data, response
, error) in
guard let data = data else { return }
do {
let decoder = JSONDecoder()
let weatherData = try decoder.decode(MyWeather.self, from: data)
You need to remove this
case weather
as there is no var for it also use CodingKeys only if you'll change key name , the Codable for the official json is
struct MyWeather: Codable {
let cod: String
let message: Double
let cnt: Int
let list: [List]
let city: City
}
struct City: Codable {
let id: Int
let name: String
let coord: Coord
let country: String
}
struct Coord: Codable {
let lat, lon: Double
}
struct List: Codable {
let dt: Int
let main: MainClass
let weather: [Weather]
let clouds: Clouds
let wind: Wind
let sys: Sys
let dtTxt: String
let rain, snow: Rain?
enum CodingKeys: String, CodingKey {
case dt, main, weather, clouds, wind, sys
case dtTxt = "dt_txt"
case rain, snow
}
}
struct Clouds: Codable {
let all: Int
}
struct MainClass: Codable {
let temp, tempMin, tempMax, pressure: Double
let seaLevel, grndLevel: Double
let humidity: Int
let tempKf: Double
enum CodingKeys: String, CodingKey {
case temp
case tempMin = "temp_min"
case tempMax = "temp_max"
case pressure
case seaLevel = "sea_level"
case grndLevel = "grnd_level"
case humidity
case tempKf = "temp_kf"
}
}
struct Rain: Codable {
let the3H: Double?
enum CodingKeys: String, CodingKey {
case the3H = "3h"
}
}
struct Sys: Codable {
let pod: Pod
}
enum Pod: String, Codable {
case d = "d"
case n = "n"
}
struct Weather: Codable {
let id: Int
let main: MainEnum
let description: Description
let icon: String
}
enum Description: String, Codable {
case brokenClouds = "broken clouds"
case clearSky = "clear sky"
case fewClouds = "few clouds"
case lightRain = "light rain"
case moderateRain = "moderate rain"
}
enum MainEnum: String, Codable {
case clear = "Clear"
case clouds = "Clouds"
case rain = "Rain"
}
struct Wind: Codable {
let speed, deg: Double
}