-4

I have two classes Model and View Controller. Model is providing value for View Controller and View Controller is returning new value to Model.

  class Model : NSObject {
         var session : Int = 0 
         }

  class ViewController : WKInterfaceController {

        var newSession = Model().session
        let counter = 5

        for var index = 0; index < counter.count ; ++index {
            newSession++
        }
   }

    print(Model.session) // How to set value of Model.session === newSession?

How to provide Model.session with the new calculated value of newSession?

Jernej Mihalic
  • 151
  • 2
  • 6
  • 1
    You need to learn what object-oriented programming is. I suggest you start right at the beginning of my book: http://www.apeth.com/swiftBook/ch01.html – matt Nov 07 '15 at 18:22

1 Answers1

1

This line is meaningless:

print(Model.session)

In fact, I'm surprised it compiles. session is an instance property, not a property of the Model class. You would need an actual instance of Model before you can talk to it. But you don't have one; in the first line you initialized one for a moment, but then you threw it away:

var newSession = Model().session

You need to retain an instance of Model so that it persists. You're not doing that. For example:

class ViewController : WKInterfaceController {
    let theModel = Model()
    // ...
}

Now you have a persistent Model instance, namely theModel. Now you can access theModel.session as much as you like.

matt
  • 515,959
  • 87
  • 875
  • 1,141