Lets say that I am writing a class that is using a URLRequest to get some JSON data from an API.
When I get the data back I create a struct within a struct to represent the structure of the data:
File: Object.swift - Class File:
struct List: Codable {
let policies: [Item]
struct Item: Codable {
let id: Int
let name: String
}
}
I then decode the JSON directly into a List item. I can then do whatever I want with it and the data is easy to access. But how can I pass it out of the class object. So lets say I want to do this in the main part of my code:
File: main.swift - Main part of code:
let myObject: Object = Object()
let dataList: List = myObject.getDataFromAPI()
for item in dataList.policies {
print(item.name)
}
Since the struct does not exist outside of the class function or class, how can I get the JSON data out of the object to do anything with? What is the right way to do this.
I think this is more of a how to program question than a I need a line of code.. but anyone who is willing to share some knowledge would be great!
Edited to clarify some points.
Ed