2
2
.
.
4
4
.
.
2
2
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
Structures
can contain Properties & Methods (just like Classes)
are Immutable since their Properties can't change (use Mutating Methods)
are Value Data Types since new copy of the Instance is given as a Parameter to a Function
can have init(...) Methods for creating Instances (Designated Initializers)
can have Mutating Methods which can change Property Values by creating new Instance (Classes don't have them)
can only adopt Protocols (can't extend Struct/Class)
can have multiple Designates Initializers init() (must set all Properties)
have Default Initializer (must set all Properties)
Content
Struct Syntax
Initializers
Mutating Methods
Adopt Protocol
S
S
t
t
r
r
u
u
c
c
t
t
S
S
y
y
n
n
t
t
a
a
x
x
Struct is declared by
using Keyword struct
followed by the Structure Name Person
followed by the Structure Body { name, age in return ("\(name) is \(age) years old") }
Struct Syntax
//DECLARE STRUCTURE.
struct Person {
//DECLARE PROPERTY.
var name : String = "unknown"
//DECLARE METHOD.
func sayHello() {
print("Hello \(name)")
}
}
//CREATE OBJECT.
var john = Person(name: "John") //Create struct Instance
john.sayHello()
print(john.name) //John
I
I
n
n
i
i
t
t
i
i
a
a
l
l
i
i
z
z
e
e
r
r
s
s
Structure can have multiple initializers with different parameters.
Designated Initializers
//DECLARE STRUCT.
struct Person {
//DECLARE FIELDS.
var name : String
var age : Int
//DECLARE INITIALIZER. ACCEPTS NO PARAMETERS.
init() {
self.name = "John"
self.age = 50
print("Created object \(name)")
}
//DECLARE INITIALIZER. ACCEPTS ONE PARAMETER.
init(age: Int) {
self.init() //Call other initializer.
self.age = age
}
}
//CREATE INSTANCE.
var bob = Person(age:50) //Call Designated Initializer.
M
M
u
u
t
t
a
a
t
t
i
i
n
n
g
g
M
M
e
e
t
t
h
h
o
o
d
d
s
s
In this example we declare struct with a mutating method that can change property values.
Calling mutating method actually creates new instance with new property value.
Mutating Methods
//DECLARE STRUCTURE.
struct Person {
//DECLARE FIELD.
var age = 0
//DECLARE MUTATING METHOD.
mutating func increaseCounter() { self.age += 1 }
}
//MUTATING FUNCTION CREATES NEW INSTANCE.
var john = Person(age: 50)
john.increaseCounter()
//EQUIVALENT TO MANUALY CREATING NEW INSTANCE.
var john2 = Person(age: 50)
john2 = Person(age: john2.age + 1)
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
Structure can adopt multiple Protocols struct Employee : Protocol1, Protocol2 { ... }.
Adopt Protocol
//DECLARE PROTOCOL.
protocol Person {
//DECLARE PROPERTIES.
var greet : String { get }
var name : String { get set }
//DECLARE METHODS.
func sayHello (name: String) -> (String)
}
//DECLARE STRUCT.
struct Employee : Person { //struct adopts Protocol
// IMPLEMENT PROTOCOL PROPERTIES.
let greet : String = "Hello"
var name : String
//IMPLEMENT PROTOCOL METHOD.
func sayHello (name: String) -> (String) {
return("\(greet) \(name)")
}
}
//CREATE STRUCT INSTANCE.
var john = Employee(name: "Unknown")
var greeting = john.sayHello(name: "John")
print(greeting)