So, let's get started with Swift. A basic class looks likes this:

brush: swift
class PersonQueue : ParentClass, Protocol1, Prot2 {

  // Class constants
  // Not currently supported by Swift

  // Member variables
  var numberOfPersons = 3  // Initialized here
  var creatorName : String // To be initialiazed in init

  var enabled : Bool  = true {
     willSet (newVal) {
     }
     didSet {            
     }
  }

  // Computed properties
  var count : Int {
     set (newValue) { // newValue is default and can be omitted
        numberOfPersons = newValue
     }
     get {
        return numberOfPeople
     }
  }
  var overflow : Bool { // get is omitted
    return numberOfPeople > 5
  }

  lazy var background : UIImage = UIImage.fromFile()

  // Instance constants
  let other : Int = 0
  let id : Int  // Must be initialized in init

  // Initializer (constructor in C++)
  // Must assign all instance variables
  init(creator : String) {
    self.creatorName = creator
    self.id = 0
  }

  // A member function
  func getCreator() -> String {
    return self.creatorName
  }

  // A class function
  static nameOfClass() -> String {
    return "This class has no name"
  }
}

I hope I can remember all this syntax... I'm getting too old to keep all this in my brain.

1 Comment