Resolved Should a class have an array of the instances made of it?

Discussion in 'iOS Programming' started by grandM, Aug 17, 2015.

  1. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #1
    I was fumbling around with an own class. I tried to let the class keep an array of all objects created with that class. For instance
    Code:
    class Wife {
    var id: UInt
    var existingWives:[Wife]?
    init(){
    if let _ = self.existingWives {
    self.id = UInt(self.existingWives!.count) + 1
    self.existingWives!.append(self)
    } else {
    self.id = 1
    self.existingWives = [self]
    }
    }
    }
    
    This doesn't work though? Or is it a no go to enter an array of its owns objects within the class?
     
  2. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #2
    I solved it. Putting static before the array var made it a class variable. Not sure though if an class should hold an array of its own instances.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    When do you remove your wife's from the list?
     
  4. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #4
    It got solved. Once again thanks :)
     

Share This Page