Grant Park Grant Park - 4 years ago 157
Swift Question

Advice about passing global data with Swift

I've got an array of objects in ViewControllerA that need to be passed to ViewControllerE, which happens to be a view controller that is "several segues away." In other words, I cannot use a segue to pass my data. I've also tried using the protocol/delegate pattern, but realized that it's only for passing BACK data. In this case, I'm trying to pass FORWARD data.

Does anyone have advice on what I should do to get my data from VC-A to VC-E?

Answer Source

Your question is already your answer, make it global, create a singleton class that hold the array of objects, then pass value to it and use it anywhere u want in your app

class Data {
    static let sharedData = Data()
    var array = [Object]()

In VC-A :

Data.sharedData.array = yourArray

Then retrieve it anywhere with let array = Data.sharedData.array

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download