codable

Parsing a Stock API that contain dates as keys using the Codable protocol

谁说我不能喝 提交于 2021-02-04 06:23:01
问题 I am trying to parse a stock api from Alpha Vantage . This is what a response looks like: API Response Demo I set up Four classes to use for decoding and encoding: Stocks Meta Data Time Series Open High Low Close I think the problem lies in the Time Series Classes since i am suppose to be getting an array dates as a Key, which each contain Open, Close, High, Low values. All three classes conform to the Codable protocol.I changed the value of the keys in the enum so that it could match the

How can I read in an array of mixed types from a JSON file in Swift?

谁都会走 提交于 2021-01-29 18:23:08
问题 I am currently trying to create a movie search app in Xcode and am having trouble reading in the JSON file from IMDB's API. API Example: https://sg.media-imdb.com/suggests/h/h.json I can read in almost the entire file except for the image information ('i') which is stored as an array with a URL (string) and two optional ints for the dimensions. struct Response: Codable { var v: Int var q: String var d: [item] } struct item: Codable { var l: String var id: String var s: String var i: ?? } No

Swift Rest API Call with Codable returning array

余生颓废 提交于 2021-01-29 14:56:45
问题 This is a further link on this question Swift Rest API call example using Codable When I change the code to handle an array, I'm getting a JSON error. Here's what I'm doing. What am I doing wrong? It errors with "error with json". // Transaction Class import Foundation final class Transaction: Codable { var id: Int? var date: Date var amount: Int var planeID: Int var userID: UUID var month: Int var year: Int init(date: Date, amount: Int, planeID: Int, userID: UUID, month: Int, year: Int) {

Swift Rest API call example using Codable

◇◆丶佛笑我妖孽 提交于 2021-01-28 05:02:08
问题 I am following a tutorial on REST API calls with Swift and Codable. I cannot compile the following although I was careful when I typed all of it. Can anyone tell me what's wrong? Also, can anyone point me to a better tutorial? The error is: Catch block is unreachable and also cannot find json in scope import UIKit import Foundation struct Example: Codable { let userId: Int let id: Int let title: String let completed: Bool } class ViewController: UIViewController { override func viewDidLoad()

How to convert custom object to Data Swift

假装没事ソ 提交于 2021-01-27 20:05:22
问题 I have a custom struct that has properties of types that are other structs with non codable types. It is a complex tree of structs within structs, and I need to convert it to Data so I can save it. I need to save this struct (chat) struct Chat{ var dictOfRecentMessages:[String:Message] = [:] var matchPicture:UIImage? var metadata:JSON? } struct Message{ let messageID:String var chatID:String let whoSent:String let timeTriedToSend:Int var messageString:String? var image:ImageMessage? var video

Decode URLs with special characters in Swift

社会主义新天地 提交于 2021-01-27 19:08:18
问题 An API I work with provides URL links, that can contain special characters like "http://es.dbpedia.org/resource/Análisis_de_datos" (the letter " á " inside). It's an absolutely valid URL, however, if a decodable class contains an optional URL? variable, it can't be decoded. I can change URL? to String? in my class and have a computed variable with something like URL(string: urlString.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed) but perhaps there is a more elegant solution

Fastest way to save structs iOS / Swift

孤街浪徒 提交于 2021-01-20 07:13:36
问题 I have structs like struct RGBA: Codable { var r: UInt8 var g: UInt8 var b: UInt8 var a: UInt8 } I want save large amount of this structs (>1_000_000) Decode guard let history = try? JSONDecoder().decode(HistoryRGBA.self, from: data) else { return } Encode guard let jsonData = try? encoder.encode(dataForSave) else { return false } How can I improve encoding/decoding time and amount of RAM memory? 回答1: Considering that all your properties are UInt8 (bytes) you can make your struct conform to

Fastest way to save structs iOS / Swift

狂风中的少年 提交于 2021-01-20 07:13:09
问题 I have structs like struct RGBA: Codable { var r: UInt8 var g: UInt8 var b: UInt8 var a: UInt8 } I want save large amount of this structs (>1_000_000) Decode guard let history = try? JSONDecoder().decode(HistoryRGBA.self, from: data) else { return } Encode guard let jsonData = try? encoder.encode(dataForSave) else { return false } How can I improve encoding/decoding time and amount of RAM memory? 回答1: Considering that all your properties are UInt8 (bytes) you can make your struct conform to

Hot to decode JSON data that could and array or a single element in Swift?

自闭症网瘾萝莉.ら 提交于 2021-01-15 19:19:55
问题 I have a struct named Info which is decoded based on the data it receives. But sometimes, one of the values in data can either be a double or an array of double. How do I set up my struct for that? struct Info: Decodable { let author: String let title: String let tags: [Tags] let price: [Double] enum Tags: String, Decodable { case nonfiction case biography case fiction } } Based on the url, I either get price as a double { "author" : "Mark A", "title" : "The Great Deman", "tags" : [

Hot to decode JSON data that could and array or a single element in Swift?

╄→гoц情女王★ 提交于 2021-01-15 19:15:06
问题 I have a struct named Info which is decoded based on the data it receives. But sometimes, one of the values in data can either be a double or an array of double. How do I set up my struct for that? struct Info: Decodable { let author: String let title: String let tags: [Tags] let price: [Double] enum Tags: String, Decodable { case nonfiction case biography case fiction } } Based on the url, I either get price as a double { "author" : "Mark A", "title" : "The Great Deman", "tags" : [