Reputation: 2633
From the "Learn the Essentials of Swift" playground, there's an example protocol:
protocol ExampleProtocol {
var simpleDescription: String { get }
func adjust()
}
There's a short passage after this example that reads:
Note: The { get } following the simpleDescription property indicates that it is read-only, meaning that the value of the property can be viewed, but never changed.
Additionally an example is given of a class conforming to this protocol:
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
However how does this class conform to the protocol? What's stopping me from mutating simpleDescription
? What don't I understand?
Upvotes: 6
Views: 4422
Reputation: 9505
The explanation is wrong:
Note: The { get } following the simpleDescription property indicates that it is read-only, meaning that the value of the property can be viewed, but never changed.
The { get }
states that any entity that wants to conform to the protocol needs to provide AT LEAST a getter for simpleDescription.
A way for the protocol to specify a getter only would be to replace the var
with a func
:
fun getSimpleDescription() -> String
Upvotes: 0
Reputation: 11
Found this and thought it may be of interest in addition to the excellent answer already provided:
If a protocol requires a property to be gettable and settable, that property requirement cannot be fulfilled by a constant stored property or a read-only computed property. If the protocol only requires a property to be gettable, the requirement can be satisfied by any kind of property, and it is valid for the property to be also settable if this is useful for your own code.
Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2.2).” iBooks. https://itun.es/us/jEUH0.l
Upvotes: 1
Reputation: 24439
Protocols place requirements on object's interface, but do not restrict implementation from providing more operations than that.
In this example, the protocol requires a readable simpleDescription
property and adjust()
method. The class provides that, so it conforms to the protocol. In other words, this protocol says implementation must have get
operation, but it does not prohibit them from also having set
.
You will not be able to mutate simpleDescription
via that protocol interface, because it does not provide such operation, but nothing prevents you from mutating it through different interface — in this example, the interface of the implementing class.
Upvotes: 3
Reputation: 14824
There's no way to specify in a protocol that you must have a read-only property. Your protocol asks for a simpleDescription
property, and allows but does not require a setter.
Note also that the only reason you may mutate simpleDescription
is because you know your a
is of type SimpleClass
. If we have a variable of type ExampleProtocol
instead...
var a: ExampleProtocol = SimpleClass()
a.simpleDescription = "newValue" //Not allowed!
Upvotes: 13