Reputation: 101
Im still trying to learn Swift so bear with me..
Essentially I have a model class that gets data from a URLSession from a website. I want to parse the json and store variables in this class, and then access the variables from my viewController.
I'm having trouble doing this, basically if I print out the array at the end of the parsing function in the model class, they get printed correctly. However, if I create a variable in the viewController class that calls the models function, the array that gets returned is empty.
I can paste my code when I get a chance, but I was just curious if anyone knew a good tutorial/practice for doing this.
Thank you
Upvotes: 0
Views: 47
Reputation: 2661
You should separate the HTTP Request
from the Model
. The HTTP Request
should be done separately, the implementation technique is upto you since it's debated which is the best way to approach it, but ultimately once you get the JSON
from this HTTP object
, you should then instantiate the model object
with the json
in order to set the class variables
.
Upvotes: 1
Reputation: 699
Like @Jay said, you should use a network Request to get the data separately, then you need to use protocol
to pass data to your ViewController
.
I use SwiftyJSON
to cast the json object, but you can create your own as well.
Upvotes: 1