Hi. In this example:
protocol ExampleProtocol {
var simpleDescription: String {
get }
mutating func adjust()
}
class SimpleClass: ExampleProtocol {
var simpleDescription: String = "A very simple class."
var anotherProperty: Int = 69105
func adjust() {
simpleDescription += " Now 100% adjusted."
}
}
var a = SimpleClass()
a.adjust()
let aDescription = a.simpleDescription
struct SimpleStructure: ExampleProtocol {
var simpleDescription: String = "A simple structure"
mutating func adjust() {
simpleDescription += " (adjusted)"
}
}
var b = SimpleStructure()
b.adjust()
let bDescription = b.simpleDescription
Correct me if I'm wrong- protocol in Swift is defined as a blueprint of a class or a struct and class and structs are blueprints of objects ( correct me if the objects I mention are lacking: like var simpleDescription, var anotherProperty and var a) which are instances of a class or a struct.
Is protocol like parameters in functions? How come you still need to declare (is it declared?) a protocol ExamplePrototcol which has var simpleDescription and mutating func adjust if var simpleDescription and func adjust() are already written in class SimpleClass: ExampleProtocol?
Also why was mutating func adjust() needed to be mutated in struct SimpleStructure? When exactly did func adjust() mutate or change it's value? What was it's value before it was mutated or changed (if change is another way of saying mutating).
By they was is String a struct or a class made by Apple programmers?
Thank you in advance.
God bless, Proverbs 31
Correct me if I'm wrong- protocol in Swift is defined as a blueprint of a class or a struct and class and structs are blueprints of objects ( correct me if the objects I mention are lacking: like var simpleDescription, var anotherProperty and var a) which are instances of a class or a struct.
No, the protocol is not the class or struct definition ; it defines requirements that a class or struct must conform to, but not limited to this;
A good example is given in Swift book, next page after your examples
extension Int : ExampleProtocol
Is protocol like parameters in functions? How come you still need to declare (is it declared?) a protocol ExamplePrototcol which has var simpleDescription and mutating func adjust if var simpleDescription and func adjust() are already written in class SimpleClass: ExampleProtocol?
No.
When you define a protocol, you say : each class that conforms to will have to provide func implementation and define how to instantiate var. Anyone who uses the class knows the class will provide those var or func.
But this implementation will depend on each class or struct.
A good example is provided later with computation of the surface of a form.
Also why was mutating func adjust() needed to be mutated in struct SimpleStructure? When exactly did func adjust() mutate or change it's value? What was it's value before it was mutated or changed (if change is another way of saying mutating).
Explanation is given directly in the book : needed for struct because data usually cannot be mutated (copy by value)
Notice the use of the mutating keyword in the declaration of SimpleStructure to mark a method that modifies the structure. The declaration of SimpleClass doesn’t need any of its methods marked as mutating because methods on a class can always modify the class.
In your case, the implemntation of ExampleProtocol changes the struct (simpleDescription is changed)
struct SimpleStructure: ExampleProtocol {
var simpleDescription: String = "A simple structure"
mutating func adjust() {
simpleDescription += " (adjusted)"
}
}