1

I have two swift files. one file has a variable with some data. I need to use data in that variable from another class.

Class A

class HVListConnectionsRequest: HVRequest {

    var connections = [HVConnection]()
    //Some code
}

Class B

class HVListConnectionsController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
}

Please tell me how can I print data in connections variable in Class B.

I am not using storyboard and I am using XIB files and MVC design pattern, so please tell me how can do it without using segues.

Caleb
  • 124,013
  • 19
  • 183
  • 272
Dimuth
  • 713
  • 2
  • 11
  • 34
  • global variable? or maybe a struct with all the vars you need on both of them – Leo Dabus Jan 02 '16 at 02:40
  • @LeoDabus i can not use this as static variable because i am appending data to connections on the instance – Dimuth Jan 02 '16 at 02:44
  • You can always add a willSet and do whatever you need before changing its value http://stackoverflow.com/a/34486265/2303865 – Leo Dabus Jan 02 '16 at 02:44
  • In additions to Leo:s tips above, possibly using a custom protocol for delegate callbacks between from HV-class to some delegate object in your view controller (possibly do callbacks using property observers as mentioned by Leo, e.g. willSet). See e.g. https://www.andrewcbancroft.com/2015/04/08/how-delegation-works-a-swift-developer-guide/ – dfrib Jan 02 '16 at 02:47
  • you should create a new var in class B make an instance of your class B in Class A , then set it ( before push or present) – Mo Farhand Jan 02 '16 at 06:38

2 Answers2

1

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 HVListConnectionsRequests 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.

Caleb
  • 124,013
  • 19
  • 183
  • 272
-1

You can use NSUserDefaults to store connection data. and On another Class B you can get Connection data from NSUserDefaults.

Too see how you can save and get value in NSUserDefaults please check this answer.

Community
  • 1
  • 1
Parth Pandya
  • 1,460
  • 3
  • 18
  • 34