Search code examples
jsonswiftibaction

Swift unable to pull information from other Swift files unless in @IBAction


I'm currently working with the latest version of Swift. In a nutshell, I'm pulling information from a webpage and storing said information into an array. Here is how I did that (forgive the indenting..):

class TransactionData {

var transactions: [Transaction] = []

init() {
    getTransactionData()
}

func getTransactionData() {
    let jsonUrl = "php file with json"

    let session = NSURLSession.sharedSession()
    let shotsUrl = NSURL(string: jsonUrl)

    let task = session.dataTaskWithURL(shotsUrl!) {
        (data, response, error) -> Void in

        do {

            let jsonData: NSArray = (try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as? NSArray)!

            for var index = 0; index < jsonData.count; ++index {

                let orderID: String = jsonData[index]["orderID"] as! String
                let orderDate: String = jsonData[index]["orderDate"] as! String
                let orderType: String = jsonData[index]["orderType"] as! String
                let paymentType: String = jsonData[index]["paymentType"] as! String
                let itemName: String = jsonData[index]["itemName"] as! String
                let itemPrice: String = jsonData[index]["itemPrice"] as! String
                let itemTaxes: String = jsonData[index]["itemTaxes"] as! String
                let orderModifications: String = jsonData[index]["orderModifications"] as! String
                let orderVariations: String = jsonData[index]["orderVariations"] as! String

                let transaction = Transaction(orderID: orderID, orderDate: orderDate, orderType: orderType, paymentType: paymentType, itemName: itemName, itemPrice: itemPrice, itemTaxes: itemTaxes, orderModifications: orderModifications, orderVariations: orderVariations)

                self.transactions.append(transaction)
            }
        } catch _ {
            // Error
        }
    }
    task.resume()
}

When I want to call the information, I use this:

let transactionData = TransactionData()
for transaction in transactionData.transactions {
     print("\(transaction)")
}

The only time information gets pulled through to a ViewController is when I'm using an IBAction. If I try anywhere else, it doesn't read the information through. For example, I'm trying to pull information from the online website to pass into a TableViewController. It just won't pull the information.

Any ideas?


Solution

  • You need to establish a connection between your ViewControllers so they can pass data between them. One common way is through a delegate protocol that is set during a segue between screens. Here is a tutorial on it.

    To expand on this a little bit, it sounds like you have a class Transactions that is called by your ViewController to load data. If you then try to access a new instance of that in your TableViewController, you don't have the data, because a new instance is being created. There are two ways to avoid this issue:

    1. Pass data (or a reference to your Transactions) from VC -> TVC via delegate.
    2. Use a singleton pattern for your data model so that all can access it.

    To avoid concurrency issue, I would suggest doing the former.