Original: http://coolketang.com/staticCoding/5a9925ad9f5454507417fc90.html
1. The get and set keywords are known as computed properties. Its purpose is to execute the code inside when taking and assigning values. This lesson will teach you how to use these two keywords.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-25996020303b3ef0.png)
2. First define a class with a specified name, as the game player.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-801d35e376a2145d.png)
3. Then add a name attribute to the class and set its default value.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-6159d8764e1b689b.png)
4. Add another level property and set its default value.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-505f000f90952f50.png)
5. Add a third attribute as the player's score, and add a pair of braces at the end to add the code that needs to be executed.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-88e343bd135955d0.png)
6. First add a get statement, when getting the value of the score, return the product of the player's level and 2.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-db862863d934b249.png)
7. Then add a set statement. When setting the value of the score, it also sets the player's level attribute.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-3862c0c2c4199d9f.png)
8. After creating the type, instantiate an object of the class.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-5f4195c7c8b95ea0.png)
9. Then obtain the score attribute of the object. At this time, it can be seen from the real-time feedback result on the right that the value of the score is 6.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-506b0f289dad6df0.png)
10. Then set the player object's score to 4.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-5bf5499251e660ed.png)
11. At this time, the level of the player object has also changed, and the value of the level attribute has been changed from the default value of 3 to 2.
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-8d650dc931b67246.png)
![Swift4 Interactive Tutorial](http://upload-images.jianshu.io/upload_images/5981183-4df9e4c06fac6714.png)
This article is organized from: "Swift4 Interactive Tutorial", the real
[Hands-on] teaching mode, get started with iOS development and Swift language at the fastest speed, free download from Apple Store App Store:
https://itunes.apple.com/cn/app/id1320746678
, or scan the QR code at the bottom of this page . Course supporting material download address:
data download