2
2
.
.
4
4
.
.
3
3
C
C
l
l
a
a
s
s
s
s
e
e
s
s
I
I
n
n
f
f
o
o
Classes
can contain Properties & Methods (just like Structures)
are mutable since Properties can change
are Reference Data Type since reference to existent Instance is given as a Parameter to a Function
can extend single Parent Class
can adopt multiple Protocols class Person : Parent, Protocol1, Protocol2 { ... }
must have at least one init(...) Method to create its Instance (Designated Initializer)
can have multiple Convenience Initializers convenience init(age:Int) { ... } and single Deinitializer deinit{...}.
SWIFT has no support for Object Literals (you cannot use Literal to create Class Instance).
When Instance is created all Properties must have value (set by Initializers or by Literals as part of their Declarations).
Content
Class Syntax
Extend Class
Adopt Protocols
C
C
l
l
a
a
s
s
s
s
S
S
y
y
n
n
t
t
a
a
x
x
Class is declared by
using Keyword class class
followed by the Class Name Person
followed by the Class Body { ... }
which must have Initializer init() (Method used to create Class Instance)
which can have Properties var name : String (Variables, Constants, Enumerators,...)
which can have Methods func sayHello() { ... }
Class Syntax
//DECLARE CLASS.
class Person {
//DECLARE PROPERTIES.
var name : String
var age : Int
//INITIALIZER.
init(name: String, age: Int) {
self.name = name
self.age = age
}
//DECLARE METHODS.
func sayHello() {
print("\(name) is \(age) years old")
}
}
//CREATE OBJECT.
var john = Person(name: "John", age: 20) //Create Class Instance by calling its init() Method
john.sayHello() //John is 20 years old
print(john.name) //John
E
E
x
x
t
t
e
e
n
n
d
d
C
C
l
l
a
a
s
s
s
s
Child Class can extend single Parent Class by adding : ParentClassName after Child Class Name.
Use Keyword super to access Parent's Properties and Methods super.displayName().
Use Keyword override to create Method or Computed Property that has the same signature as an existing Parent's one.
Inherit Class
//DECLARE PARENT CLASS.
class Person {
var name = "John"
func displayName() { print(name) }
}
//DECLARE CHILD CLASS.
class Soldier : Person {
override func displayName() { print("Hello \(name)") } //Override Method
func greet() { super.displayName() } //Call Parent's Method
}
//ACCESS PARENT FIELDS AND METHODS.
var john = Soldier() //Create Object from Class Soldier.
john.displayName() //Reference overriden method.
var name = (john.name) //Reference inherited field.
print(name)
A
A
d
d
o
o
p
p
t
t
P
P
r
r
o
o
t
t
o
o
c
c
o
o
l
l
s
s
Class can adopt multiple Protocols by implementing their Properties and Methods.
Adopt Protocol
//DECLARE PROTOCOL.
protocol Employee {
//DECLARE PROPERTIES.
var greet : String { get }
var name : String { get set }
//DECLARE METHODS.
func sayHello (name: String) -> (String)
}
//DECLARE CLAS.
class Soldier : Employee {
//DECLARE PROTOCOL PROPERTIES.
let greet : String = "Hello"
var name : String
//DECLARE PROTOCOL METHOD.
func sayHello (name: String) -> (String) {
return("\(greet) \(name)")
}
//INITIALIZER.
init(name: String) {
self.name = name
}
}
//CREATE STRUCT INSTANCE.
var john = Employee(name: "Unknown")
var greeting = john.sayHello(name: "John")
print(greeting)