Manos Serifios Manos Serifios - 3 months ago 6x
iOS Question

Class create an instance of itself

i want an instance method of a class to create an instance of itself
and append it to an array.

I tried this:

var vampireArray: [Vampire] = []

class Vampire {

func createSelf() {




but i get a strange error on console like (lldb)

Any ideas?


Even without knowing the context to this code, in general, creating and then appending an instance of a class to an array outside of the scope of the class is a bad idea.

Instead, try putting the line vampireArray.append(Vampire()) wherever you were originally planning to put createSelf().

If you wanted to stick with this route, it would seem like the method createSelf() should be a static method and var vampireArray: [Vampire] = [] should also be a static variable inside your class.


If all you want to do is keep track of the number of vampires, this could be a good solution:

class Vampire {
    static var vampireCount = 0

        vampireCount += 1