0

In Swift, I am trying to build a simple app that runs the card game war. There are two view controllers, one for normal gameplay and one in case of a tie. So, in my code, I have two different classes, one called mainScene and one called tieScene. To store which cards the player has and which cards the enemy has, I use two arrays named playerArray and enemy Array. If a tie occurs, I segue from mainScene to tieScene. How do I incorporate the information for playerArray and enemyArray from class mainScene, and use these values in class tieScene?

One thing I tried was subclassing tieScene as part of mainScene as follows:

 class tieScene: mainScene {...}

This allowed me to use the playerArray and enemyArray in class tieScene. However, the option to connect tieScene to my second view controller in Interface Builder was not there when I subclassed tieScene.

So, I have two questions:

  1. Did I subclass tieScene wrong and is that why I couldn't connect tieScene in my interface builder?

  2. Besides subclassing, how can I use the information from one class in another class?

jbcd13
  • 145
  • 8

3 Answers3

0

I don't think you're understanding what a subclass is.. If you're subclassing, then that just means that the child class has the same properties as the super class, so it will have the arrays but they are not persisted.

There are a couple of things you can use, the easiest would be to simply create the arrays in your tie view, then when you create a new tie view, set the arrays. Or create a method to setup the tie view with the arrays(initWithEnemyArray.....)

JDM
  • 883
  • 1
  • 8
  • 20
0

You should not be using subclassing like this, subclassing is not used to share information, that is a misuse of what object orientation is about. It would not work because when your program runs you will have a separate instance of type tieScene and another instance of mainScene. Therefore you will have two separate instances of the data, not one instance which is shared. You should consider making a model class and storing your data there. Read about MVC.

Regarding OOP and inheritance, make sure you understand the difference between a class and an instance of a class. You are mixed up thinking an instance and a class are the same thing, they are not.

Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
0

This is OOP 101 stuff.

Subclassing does not share information.

Cars and trucks are both motor vehicles

Sedans and coupes are both subclasses of car. Say both have a radio with a radio station property.

If I change the radio station in my sedan instance, that does not mean you can query the radio station in your coupe and find out what station I'm listening to.

Separate objects are completely separate from each other, and have their own instance variables.

You can either set up a link between your two objects and define a protocol to communicate between them, or if you want to save global app state information then maybe you need a data container singleton. See this thread I created for a discussion of the different options:

How do you share data between view controllers and other objects in Swift?

Community
  • 1
  • 1
Duncan C
  • 128,072
  • 22
  • 173
  • 272