I'm learning Swift and I'm reading The Swift Programming Language from Apple. I don't have any Objective-C background (only PHP, JavaScript, and others, but not Objective-C).
On page 24-25 I see this code:
//...Class definition stuff... var perimeter: Double { get { return 3.0 * sideLength } set { sideLength = newValue / 3.0 } } //...Class continues...
This part is not specified in the book, and I can't get what those are for.
What are get and set?
Swift 4 sets are used to store distinct values of same types but they don't have definite ordering as arrays have. You can use sets instead of arrays if ordering of elements is not an issue, or if you want to ensure that there are no duplicate values. (sets allow only distinct values.)
A lazy stored property is a property whose initial value isn't calculated until the first time it's used. You indicate a lazy stored property by writing the lazy modifier before its declaration.
There are two kinds of properties: stored properties and computed properties. Stored properties are properties that are stored in the class's instance. Stored properties store constant and variable values. Computed properties are for creating custom get and set methods for stored properties.
The getting and setting of variables within classes refers to either retrieving ("getting") or altering ("setting") their contents.
Consider a variable members
of a class family
. Naturally, this variable would need to be an integer, since a family can never consist of two point something people.
So you would probably go ahead by defining the members
variable like this:
class family { var members:Int }
This, however, will give people using this class the possibility to set the number of family members to something like 0 or 1. And since there is no such thing as a family of 1 or 0, this is quite unfortunate.
This is where the getters and setters come in. This way you can decide for yourself how variables can be altered and what values they can receive, as well as deciding what content they return.
Returning to our family class, let's make sure nobody can set the members
value to anything less than 2:
class family { var _members:Int = 2 var members:Int { get { return _members } set (newVal) { if newVal >= 2 { _members = newVal } else { println('error: cannot have family with less than 2 members') } } } }
Now we can access the members
variable as before, by typing instanceOfFamily.members
, and thanks to the setter function, we can also set it's value as before, by typing, for example: instanceOfFamily.members = 3
. What has changed, however, is the fact that we cannot set this variable to anything smaller than 2 anymore.
Note the introduction of the _members
variable, which is the actual variable to store the value that we set through the members
setter function. The original members
has now become a computed property, meaning that it only acts as an interface to deal with our actual variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With