3
3
.
.
4
4
S
S
t
t
r
r
u
u
c
c
t
t
u
u
r
r
e
e
s
s
I
I
n
n
f
f
o
o
This chapter covers complex structures: Tuple, Struct, Class.
They can be extended using: Protocol, Extensions.
T
T
u
u
p
p
l
l
e
e
Tuple Data Type contains list of Parameters.
Variable can have named Parameters. When storing values using Parameter names order doesn't matter.
Tuples
//DECLARE & INITIALIZE TUPLE VARIABLE.
var error1 = (1, "RED") //Implicit data types.
var error2 : ( Int, String) = (1, "RED") //Explicit data types. Same order.
var error3 : (code: Int, message: String) = (1, "RED") //Named data types. Same order.
var error4 : (code: Int, message: String) = (message:"RED", code:1) //Named data types. Any order.
//DECLARE TUPLE VARIABLE. STORE DATA LATER.
var error : (code: Int, message: String) //Named data types. No nitialization.
error = (1, "RED")
error = (message:"RED", code:1) //Named data types. Any order.
error.0 = 1
error.code = 1
//REFERENCE TUPLE & ITS VALUES.
print(error) //(code: 1, message: "RED")
print(error.0) //1 Reference Parameter using index.
print(error.message) //RED Reference Parameter using name.
//EXTRACT TUPLE VALUES.
var (myCode, myMessage) = error //Store Tuple Parameters into Variables. Order is important.
S
S
t
t
r
r
u
u
c
c
t
t
When creating Instance you can set Properties using Initializers (must set all Properties that have no initial values)
Default Initializer (it is used only if there are no Dedicated Initializers)
Designated Initializers init(parameters) { ... } (Can call other Designated Initializers)
Only mutating Methods can change Property values (new instance is created with new values).
Struct can only adopt Protocols (can't extend Struct/Class).
Default Initializer & Mutating Methods
//DECLARE STRUCTURE.
struct Person {
//PROPERTIES.
var name : String //Value must be set through Default Initializer
var age : Int = 0 //Has initial value.
//METHODS.
mutating func greet () -> (String) { //Mutating Method changes age
self.age = 20 //Otherwise 'self' is immutable
return("\(name) is \(age) years old")
}
}
//CREATE INSTANCE.
var john = Person(name: "John") //Call Default Initializer (Create Instance)
print(john.greet()) //John is 20 years old
john.name = "Bill" //You can change Property Value
print(john.name) //Bill
print(john.age ) //20
Designated Initializers
//DECLARE STRUCT.
struct Person {
//DECLARE FIELDS.
var name : String //Value must be set through Designated Initializer
var age : Int //Value must be set through Designated Initializer
//DESIGNATED INITIALIZER (with 2 parameters)
init(name: String, age: Int) {
self.name = name
self.age = age
}
//DESIGNATED INITIALIZER (with 1 parameter)
init(name: String) {
self.init(name: name, age: 50) //Call Designated Initializer with default age
}
}
//CREATE INSTANCE.
var person1 = Person(name: "John", age: 20) //Call Designated Initializer.
var person2 = Person(name: "Bill") //Call Designated Initializer.
dump(person2)
C
C
l
l
a
a
s
s
s
s
Class must have initializer if any of the Properties are missing default values
Designated Initializers init (parameters) { body }
(Can't reference other Initializers in the same Class. Can reference Parent's Designated Initializer)
Convenience Initializers convenience init (parameters) { body }
(Can reference other Convenience Initializers in the same Class. Chain must end referencing Designated Initializer)
Initializers
//DECLARE STRUCT.
class Person {
//DECLARE FIELDS.
var name : String //Value must be set through Initializers
var age : Int //Value must be set through Initializers
//DESIGNATED INITIALIZER (with 2 parameters)
init(name: String, age: Int) {
self.name = name
self.age = age
}
//DESIGNATED INITIALIZER (with 1 parameter)
convenience init(name: String) {
self.init(name: name, age: 50) //Call Designated Initializer with default age
}
}
//CREATE INSTANCE.
var person1 = Person(name: "John", age: 20) //Call Designated Initializer.
var person2 = Person(name: "John") //Call Designated Initializer.
dump(person2)
P
P
r
r
o
o
t
t
o
o
c
c
o
o
l
l
Protocols declare Properties and Methods without implementation. (similar to Interface in other Languages)
Multiple Protocols can be adopted by Classes, Structures or Enumerators.
Implement Protocol
//DECLARE PROTOCOL.
protocol Person {
//DECLARE COMPUTED PROPERTIES.
var name : String { get set } //Can be implemented as Variable. Only var is valid.
var age : Int { get } //Can be implemented as Constant. Only var is valid.
//DECLARE METHODS.
func greet () -> (String)
}
//DECLARE STRUCT.
struct Employee : Person { //struct adopts Person Protocol
//DECLARE PROPERTIES.
var name : String //Implemented as Variable
let age : Int //Implemented as Constant
//DECLARE METHOD.
func greet () -> (String) { return("\(name) is \(age) years old ") }
}
//CREATE STRUCT INSTANCE.
var employee = Employee(name: "John", age: 20)
print(employee.greet())
E
E
x
x
t
t
e
e
n
n
s
s
i
i
o
o
n
n
s
s
Extensions are used to add Computed Properties and Methods to: Classes, Structures, Enumerations or Protocols.
Extensions
//DECLARE STRUCT.
struct Employee {
//DECLARE PROPERTIES.
var name : String
var age : Int
}
//DECLARE EXTENSION.
extension Employee {
//ADD COMPUTED PROPERTIES.
var hello : String {
get { return("Hello \(name)") }
}
//DECLARE METHODS.
func greet () -> (String) { return("\(name) is \(age) years old") }
}
//REFERENCE EXTENSION'S COMPUTED PROPERTIES & METHODS.
var employee : Employee = Employee(name: "John", age: 20)
print(employee.hello)
print(employee.greet())