Quick, proper use of getters and setters

Can someone please help me understand the proper use of getters and setters in quick mode. I get the impression that this is not the same as saying Java.

Is this proper use in Swift to store and access a class variable?

class Person { private var name: String init(name: String) { self.name = name } func setName(name: String) { self.name = name } func getName() -> String { return name } } 
+5
source share
3 answers

Swift provides a much more structured approach to getters and setters , then to Java.

You can, but you must not , write setters and getters, as in your code.

Instead (if you use stored properties ) just declare the property with visibility not private (e.g. internal in my example). This way, callers outside your class will be able to see the property and change it.

 class Person { var name: String { willSet(newValue) { print("\(self.name) is going to be renamed as \(newValue)") } didSet(oldValue) { print("\(oldValue) has been renamed as \(self.name)") } } init(name: String) { self.name = name } } 

Good, but in java getter and seters allow you to add custom logic to execute before or after changing the value.

Right! In Swift, you can do this with the willSet and didSet .

willSet (new_value)

Here you write the code that you want to run before , the new value is written to the property. Here you can access the current value (which will also be overwritten) using self.name , and the new value is available using newValue .

didSet (OldValue)

Here you write the code that you want to run after , the new value is written to the property. Here you can access the old value (which has been overwritten) with oldValue , and the new value is available in self.name .

Both willSet and didSet are optional (I'm not talking about an extra type! I mean, you are not forced to write them :).

If you do not need to run any code just before or after , the property has been changed, just omit them.

Example

 let aVerySmartPerson = Person(name: "Walter White") aVerySmartPerson.name = "Heisenberg" // > Walter White is going to be renamed as Heisenberg // > Walter White has been renamed as Heisenberg 
+11
source

If you assign self., You simply call this method again. Also, there is no “get” like the old Java bean property template. Finally, if you really need to use methods for calculating properties or actions after installation, they can be built directly in the variable definition.

 class Person { private var name: String; init( name: String ) { self.name = name } } 

should be enough for your simple case, although you can also

 private var name: String { didSet { // updating something after setting } }; 
+3
source

How setter and getter work like in Java:

 class Person { private var _name private var _age // .... other properties var name: String { get { return _name } set { _name = newValue } } var age: String { get { return _age } set { _age = newValue } } } 
+1
source

Source: https://habr.com/ru/post/1237101/


All Articles