Different types of the same object

Mattias

So, I'm in Xcode and programming a small program from a friend.

I want to initialize several instances of an object, put them in an array and then iterate through it (via a function that returns a string). Each object adds some text to that string when it's iterated, depending on the variables of the object.

Let's say the class is Tree. The variables in the class are name (string), height(int) and a hasLeaves(bool)(weather it currently has leaves on it or not). I could easily print:

"This is a name that is height meters tall and has leaves.

The problem is that I want the string to be a bit different, depending on which kind of tree it is. Like this:

The oak(name) is big and lifeful, it's height meters tall and has leaves.

Apple trees(name) carries some apples, it's height meters tall and has leaves.

If you ever visit Sweden you should check out the firs(name), they are height tall and haven't got leaves.

I don't want you to write the code for me, but give me a clue. I don't know what to look for. I was thinking about creating a subclass for each Tree, but every subclass would only appear once in the program and I don't know if it's necessary or not.

As you recognize, I'm having a hard time even formulating this question, but if you understand my intentions I'm glad for any clue I can get.

Edit: So this is my attempt to show it in code:

Class:

class tree {

var treeHeight: Int?
var treeWidth: Int?
var hasLeaves: Bool
var treeString: String


init (height: Int?, width: Int?, leaves: Bool, theString: String) {

    self.treeHeight = height
    self.treeWidth = width
    self.hasLeaves = leaves
    self.treeString = theString

    }

}

Main function:

var oak: tree = tree(height: 1, width: 2, leaves: true, theString:"Oh, the mighty oak")
var appleTree: tree = tree(height: 1, width: 2, leaves: false, theString: "Yummy Apples")

var treeArray: Array = [oak, appleTree]
var resultString = "This is the stories of the trees: "


for tree in treeArray {

        if tree.hasLeaves == true {

            resultString = resultString + tree.theString
            }

        }

So, I want the string added to the "resultString" to be different, depending on what kind of tree it is, but I don't want to set that string in the initialization of the object, but rather from what "kind" of tree it is. Does that make it easier to understand?

I want the string (treeString) to be static depending on what "kind" of tree it is. So if it is an oak, the string is always "Oh, the might oak".

Steve Rosenberg

It sounds like you want a tree class with some properties like leaves, etc. Maybe you also want to subclass leaves with additional properties like color, etc. I recommend the WWDC 2014 video:

http://youtu.be/W1s9ZjDkSN0

Somewhere around 30 minutes they have a demo of a Car class with RaceCar at subclass.

Regarding creating the objects, you can build each object individually and then collect them in an array as one option. For example, maybe in a form on your app the user inputs data for a class or subclass and then you create an object, store to a master array which you then archive to a file.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using input types of the same object in different mutations

From Dev

Java - Comparator Class for same attributes from different object types

From Dev

Can two objects of different types with different alingment requirements have the same object representation?

From Dev

Different input types in the same method

From Dev

XSD different types same name

From Dev

Register same class for different types

From Dev

Same object in different classes?

From Dev

Compare different object types with comparator

From Dev

Deserializing an array of different object types

From Dev

Object data with 2 different types

From Dev

Haskell: Using the same operator on different types in a function

From Dev

JavaFX - Different css classes for same node types

From Dev

Android two different SharedPreference types with the same ID

From Dev

Interface with members with same name and different return types

From Dev

Gson - Same field name, different types

From Dev

XSD multiple elements with same name but different types

From Dev

How to accept different types of slices in the same function?

From Dev

App install size the same on different device types

From Dev

Deserializing attributes of same name but different types in Jackson?

From Dev

Mock same interface as 2 different types

From Dev

Same method in multiple classes but using different types

From Dev

core data: save different types as the same attribute

From Dev

Different types have same signatures in CIL

From Dev

C++ same function on vectors of different types

From Dev

Elasticsearch mapping - different data types in same field

From Dev

How to use the same route with different parameter types?

From Dev

Multiple methods with same functionality but different types

From Dev

Code duplication, same functionality different types

From Dev

Django: same Comment model for different Post types

Related Related

  1. 1

    Using input types of the same object in different mutations

  2. 2

    Java - Comparator Class for same attributes from different object types

  3. 3

    Can two objects of different types with different alingment requirements have the same object representation?

  4. 4

    Different input types in the same method

  5. 5

    XSD different types same name

  6. 6

    Register same class for different types

  7. 7

    Same object in different classes?

  8. 8

    Compare different object types with comparator

  9. 9

    Deserializing an array of different object types

  10. 10

    Object data with 2 different types

  11. 11

    Haskell: Using the same operator on different types in a function

  12. 12

    JavaFX - Different css classes for same node types

  13. 13

    Android two different SharedPreference types with the same ID

  14. 14

    Interface with members with same name and different return types

  15. 15

    Gson - Same field name, different types

  16. 16

    XSD multiple elements with same name but different types

  17. 17

    How to accept different types of slices in the same function?

  18. 18

    App install size the same on different device types

  19. 19

    Deserializing attributes of same name but different types in Jackson?

  20. 20

    Mock same interface as 2 different types

  21. 21

    Same method in multiple classes but using different types

  22. 22

    core data: save different types as the same attribute

  23. 23

    Different types have same signatures in CIL

  24. 24

    C++ same function on vectors of different types

  25. 25

    Elasticsearch mapping - different data types in same field

  26. 26

    How to use the same route with different parameter types?

  27. 27

    Multiple methods with same functionality but different types

  28. 28

    Code duplication, same functionality different types

  29. 29

    Django: same Comment model for different Post types

HotTag

Archive