I have two swift files. one file has a variable with some data. I need to use data in that variable from another class.
What you're really trying to do is to use data in another object. connections
is a property of HVListConnectionsRequest
, so each instance of HVListConnectionsRequest
has its own value for connections
.
Moving data between objects is a fundamental task in object oriented programming, and doing it requires that one object have a reference to another object. In this case, you need your HVListConnectionsController
object to somehow get a reference to the instance of HVListConnectionsRequest
that you care about. There are many ways to make that happen, for example:
A HVListConnectionsController
can create a new HVListConnectionsRequest
.
Some other object can give a HVListConnectionsRequest
object to an HVListConnectionsController
.
A HVListConnectionsController
can fetch a HVListConnectionsRequest
from some well-known location or object.
Once your HVListConnectionsController
has a reference to a HVListConnectionsRequest
, then getting it's connections
property is obviously simple. For example, if someRequest
points to the connections request:
let myConnections = someConnectionsRequest.connections
So, you need to think about the relationship between HVListConnectionsRequest
and HVListConnectionsController
... how are HVListConnectionsRequest
objects created? You say that you're using MVC; are HVListConnectionsRequest
s part of your app's data model, and if so, can the HVListConnectionsController
get the connections request from the model? Or are they perhaps created by the same view controller that creates the HVListConnectionsViewController
, and if so, can that view controller pass the request along when it creates a `HVListConnectionsViewController'?
In short, I can't tell you how one object in your app should talk to another without knowing more about how those objects are created in your app. The good news is that I don't need to tell you, either... once you're looking at the relationships between objects, you'll be on track to solve this problem yourself.
One thing I can tell you, though, is that you'll do well to avoid "easy" solutions like creating a singleton or storing the data in the user defaults system. You're dealing with a pretty typical OO design issue, and punting to user defaults or making everything in your app accessible to everything else simply avoid the design problem; the result will be a poorly designed app that becomes increasingly difficult to work on.