title: count number of instances of a class swift
body: <p>How do you count the number of instances of a class in Swift? I have the following class:</p>
  
  <pre><code>class car {
      static var carPopulation: Int = 0
      var color: String
      var capacity: Int
      var driver: Bool?
      var carOn: Bool = false
      init (carColor: String, carCapacity: Int) {
          self.capacity = carCapacity
          self.color = carColor
          carPopulation ++ //##########GIVES ME ERROR#############
      }
      func startCar() {
          self.carOn = true
      }
  }
  
  class raceCar : car {
      var nitro: Bool
      init(carColor: String, carCapacity: Int, hasNitro: Bool) {
          self.nitro = hasNitro
          super.init(carColor: carColor, carCapacity: carCapacity)
      }
  }
  
  car.CarPopulation //outputs 0 in xcode playground
  </code></pre>
  
  <p>I am assuming I need a function to actually return the value, but I want the counter to increase every time an instance of the class is created.</p>
  
  <p>I get the following error:</p>
  
  <p><code>error: static member 'carPopulation' cannot be used on instance of type 'car'</code></p>