Could a User of an app create a new child class, instead of a class instance

Context


I got some advice that with a particular data model, it would be best to use classes, not structs. Because using structs to store the data of a spreadsheet-like app I'm trying to make, will mean every change to any one cell, would cause the whole table data model to update at the same time, when it's not needed.

It sounds like using classes will prevent the app form inevitably crashing, bugging out, or just being supper laggy, as spreadsheets grow in size.

My problem now, is that every new instance of cell, row, and table section, needs to have its own data. Because "tile the plane with your word of choice" doesn't sound like a partially useful app. Something that needs to be avoided somehow, if I'm going to use classes, instead of structs.


The Problem


If the user hits the "Add Row" button, and that just makes a new instance of a class cellRow, then it will display the same stuff as all the other row instances.

This class behavior may be sidestepped by either:

  • Making a new child class of cellRow,
  • Or just making a new class, declared in its entirely by an addRow function.

And everything in the table/organizer being in table sections and table supper sections - means there will have to be funcs that make new classes, that have arrays of classes inside them.
( Definitely should go with the child class option based on that, if I'm not mistaken. )

As a complete beginner in coding, I'm not entirely sure how to put together such a function, if this is a viable approach?

Any help would be greatly appreciated. Thanks!

I think you might be getting confused with some of the terminology and that can make googling things difficult.

Different instances of classes or structs can have different properties. A class or struct defines the structure of a thing. Like to be a dog you need to have a name and a breed. An instance is a specific one of your class or struct. My dog harry is a terrier. So you can defs have different instances of CellRow that have different data saved inside. Try reading this

Child classes are to do with inheritance which is about making your class more specific. Really not something you need to worry about yet.

For your use case, you want to be creating new instances

1 Like

Thanks! Reading that article helped a lot!

class Bike {
  var color: String

  init(color:String) {
    self.color = color
  }
}

var bike1 = Bike(color: "Blue")

var bike2 = bike1 // < The behavior I thought was a default 
                  // behavior of classes, only happens when 
                  // you set two class instances equal to 
                  // each other. As is done here.  

bike1.color = "Red"
print(bike2.color)  // prints Red

In this example, the behavior is not present:

// define a class
class Employee {

// define a property
var employeeID = 0
}

// create two objects of the Employee class
var employee1 = Employee()
var employee2 = Employee()

// access property using employee1
employee1.employeeID = 1001
print("Employee ID: \(employee1.employeeID)") 

// access properties using employee2
employee2.employeeID = 1002
print("Employee ID: \(employee2.employeeID)")

Output:

Employee ID: 1001
Employee ID: 1002 
1 Like