I am interested in storing data in a sperate class, to allow this data to be saved and accessed from any view in the application. To do this, I first created an empty class in my project called GlobalData.swift
. In this file I have the source code below:
import UIKit
class Main {
var drinkData:PFObject?
init(drinkData:PFObject) {
self.drinkData = drinkData
}
}
In my Table View, when a user selects a cell I am doing save Parse PFObject
so it can be easily accessed later on. In my Table class I have the following code:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let indexPath = self.tableView.indexPathForSelectedRow {
let row = Int(indexPath.row)
let localDrinkData = Main(drinkData:(objects?[row] as PFObject!))
print("global : \(localDrinkData.drinkData)")
}
}
Now this works perfectly. The print
line will do exactly what I want it to do (print out the PFObject
and all items inside this row), the problem is I need to save this and then be able to access it in a different class.
My question remains. How can I call the Main
drinkData
in a different class and be able to print it exactly like I did in the function above:
print("global : \(localDrinkData.drinkData)")
Here is where I got the original source code for this design. Obviously, I made a few modifications: Access variable in different class - Swift
Thank you very much!