How can I pass my JSON response (data
) from the getStories()
function to a new variable (var stories
) which is outside of my function and after that to be able to use that new variable into my CollectionViewController ?
Here is my code:
import UIKit
class StoryCollectionViewController: UICollectionViewController {
// MARK: - ***** Properties *****
private var stories: Story!
// MARK: - ***** Life Cycles *****
override func viewDidLoad() {
super.viewDidLoad()
// Register cell class
collectionView?.register(UINib.init(nibName: "StoryCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: Constants.reuseIdentifier)
// Get Stories
getStories()
print(stories.storyID) // Here should return 15 but return NIL and I don't know why
}
// MARK: ***** UICollectionView DataSource Methods *****
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Constants.reuseIdentifier, for: indexPath) as! StoryCollectionViewCell
DispatchQueue.main.async {
//cell.storyImageView.imageFromServerURL(urlString: self.posterUrl)
//cell.storyNameLabel.text = self.stories?.name
}
return cell
}
//MARK: - ***** CollectionView Delegate Methods *****
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "showStoryDetails", sender: self.stories)
}
// Get Stories from Amazon API server
func getStories(){
Spinner.show("Loading Stories")
let defaultSession = URLSession(configuration: .default)
if let urlComponents = URLComponents(string: Constants.baseURL) {
guard let url = urlComponents.url else { return }
let dataTask = defaultSession.dataTask(with: url) { (data, _, error) in
if error != nil {
self.showMessage(message: error.debugDescription)
} else if let data = data {
do {
let data = try JSONDecoder().decode(ModelJson.self, from: data)
self.stories = data.result // This object "stories" is empty outside of this function ..........
print("Story ID: \(self.stories.storyID!)") // Here return 15 which is correct
} catch {
self.showMessage(message: error.localizedDescription)
}
}
Spinner.hide()
}
dataTask.resume()
}
}
}
Here is a screen to show you that my JSON response contains data and return 15 in console:
Thank you guys if you are reading this. Have a great day !