Hello :)
I am trying to decode a JSON stock market API then store the returned information in an instance of a struct but am getting Thread 1: signal SIGABRT on the struct instance line of code.
The JSON data is in an array of objects that would like this:
{
"pagination": {
... //Not interested in storing this info
},
"data": [
{
"open": 129.8,
"high": 133.04,
},
[...] //would be data for other symbols
]
}
Struct Declaration:
struct SingleData: Decodable {
let open: Double
let high: Double
}
struct ScrapeStruct: Decodable {
let data: [SingleData]
}
Struct Instance:
var QQQ = try ScrapeStruct(from: Decoder.self as! Decoder) //Thread 1: signal SIGABRT error
Target Output says "Could not cast value of type 'Swift.Decoder.Protocol' (0x1db05bc88) to 'Swift.Decoder'". I know that this is in reference to the .self but I tried other things like Decoder() and even SingleData() or ScrapeStruct(). But just got compiler errors about it not being initialized and that it's missing from: Decoder respectively. And with the struct containing an array and me having issues working with individual values like QQQ.high because ScrapeStruct has no member high I just have no idea what is supposed to be here.
I don't know if this part of the code is helpful but I will include it anyways just in case.
Function:
func Scraper() async throws -> ScrapeStruct {
guard let url = URL(string: "MarketStack API")
else {
throw Error.invalidServerResponse
}
var request = URLRequest(url: url)
request.addValue("My Access Code", forHTTPHeaderField: "access_key")
request.addValue("QQQ", forHTTPHeaderField: "symbols")
let (data, _) = try await URLSession.shared.data(for: request)
let scrapeStruct = try JSONDecoder().decode(ScrapeStruct.self, from: data)
return scrapeStruct
}
Call Function:
Task {
let fetchedInfo = try? await Scraper()
DispatchQueue.main.async {
if let fetched = fetchedInfo {
QQQ = fetched
}
}
}
Any help at all would be greatly appreciated. Thanks!
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hello, I am trying to learn more about the different stock market APIs and am working with Polygonio right now. I am using the swift wrapper for it by toniremi (https://github.com/toniremi/PolygonioSwift). The example on the site makes it seem like just doing this should make it work but I am still not producing any requests on Polygonio.
Code:
import UIKit
import PolygonioSwift
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let polygon = PolygonioSwift.Client(key: "Access Code")
polygon.setDebug(enable: true)
polygon.dailyOpenClose(symbol: "symbol", date: "date") { (result: DailyOpenCloseResponse?, err) in
if let err = err {
print(err)
} else {
print(result!) //Not sure why force-unwrap is necessary but XCode warns the expression is implicitly coerced from DailyOpenCloseResponse? to 'Any" without it
}
}
}
}
But without any compiler or runtime errors the simulation finishes after a few seconds and never produces any requests on Polygon.io. The package declares DailyOpenClose response and the ApiRequest so I am not sure if I need to include it on my ContentView file and even if I do I get other compiler errors but that is for another post I think(let me know and I will include that stuff if necessary).
Hello, I am trying to code an API scraper that will collect Double values in a struct then return them but am getting the title error on the return line of code.
Struct Declaration:
struct ScrapeStruct: Decodable {
var high: Double
var low: Double
var close: Double
var volume: Double
}
Error Declaration:
enum Error: Error {
case invalidServerResponse
}
Scrape Function:
class dataFetcher {
func Scraper() async throws -> ScrapeStruct {
guard let url = URL(string: "someURL")
else {
throw Error.invalidServerResponse
}
let request = URLRequest(url: url)
let (data, _) = try await URLSession.shared.data(for: request)
_ = try JSONDecoder().decode(ScrapeStruct.self, from:data)
return ScrapeStruct.init(high: Double, low: Double, close: Double, volume: Double) // error is here
}
}
I am self-taught so there's a chance there are some fundamentel misunderstandings so if that's the case any recommendations on where to learn more in place of a solution are appreciated as well because I can't find anything online that's too helpful.