Tutorial by Examples

You define a class like this: class Dog {} A class can also be a subclass of another class: class Animal {} class Dog: Animal {} In this example, Animal could also be a protocol that Dog conforms to.
Classes are reference types, meaning that multiple variables can refer to the same instance. class Dog { var name = "" } let firstDog = Dog() firstDog.name = "Fido" let otherDog = firstDog // otherDog points to the same Dog instance otherDog.name = "Rover" // modifying otherDog also...
Classes can define properties that instances of the class can use. In this example, Dog has two properties: name and dogYearAge: class Dog { var name = "" var dogYearAge = 0 } You can access the properties with dot syntax: let dog = Dog() print(dog.name) print(dog.dogYear...
Swift does not support multiple inheritance. That is, you cannot inherit from more than one class. class Animal { ... } class Pet { ... } class Dog: Animal, Pet { ... } // This will result in a compiler error. Instead you are encouraged to use composition when creating your types. This can b...
class ClassA { var timer: NSTimer! init() { // initialize timer } deinit { // code timer.invalidate() } }

Page 1 of 1