0

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

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
Edward S.
  • 331
  • 2
  • 15
  • This question is to broad and not suitable for stackoverflow – Joakim Danielson Aug 06 '18 at 18:17
  • Have you tried `for item in dataList.policies {` ? – Fabian Aug 06 '18 at 18:24
  • @JoakimDanielson I suppose it is a broad question, but given that it should have a specific answer such as X or Y is the correct way to do this and here is some example code, and given that Swift 4 made large changes to how JSON is parsed perhaps this question can help others as well if answered? – Edward S. Aug 06 '18 at 18:24
  • @Purpose I have edited to clarify but in this case how would I return the struct out of the class method? Since the struct does not exist in main.swift? – Edward S. Aug 06 '18 at 18:28
  • So the struct is defined inside `getDataFromAPI()`? – Fabian Aug 06 '18 at 18:29
  • @Purpose either inside getDataFromAPI() or inside Object.swift. I defined it because in the class I wanted to parse out the data.. but then its in this proprietary format and Im not sure how to access it from outside that method. – Edward S. Aug 06 '18 at 18:31
  • How do you want to access it from outside that method? You have to pass it out somehow, with the given format you parsed it with or with a format you define to be passed out. – Fabian Aug 06 '18 at 18:35
  • @Purpose Id want to pass it out some way that made sense like an array or dictionary based on the shape of the data. But everything I found about parsing JSON seemed to indicate you had to create a struct. – Edward S. Aug 06 '18 at 18:37
  • If you want to return Dictionary, what about [this](https://stackoverflow.com/questions/30480672/how-to-convert-a-json-string-to-a-dictionary)? Why not return „proprietary format struct“? – Fabian Aug 06 '18 at 18:41

1 Answers1

0

@Purpose provided me a clue in his comments but I found the answer here on another StackOverflow question:

accessing struct from one class to another

Turns out you can create it by doing:

variable: Object.struct = someValue

I had no idea!

Edward S.
  • 331
  • 2
  • 15