Let me first design a class: There is an account (bank accounts) class, assuming it has 3 attributes:Amount (account amount),interestrate (interest rate), and owner (account name).
of These 3 attributes, amount and owner will vary from one account to another, and the contents of each account are different, and the interestrate of all accounts are the same.
The amount and owner properties are related to the account entity, called instance properties. The interestrate attribute is independent of the individual, or shared by all account individuals, which is called a static property or type attribute.
Object-oriented types (structs, enumerations, and classes) can all define static properties, and their syntax formats are as follows:
- struct struct Name {//define struct body, can define static storage property and static computed property in struct
- static VAR (or let) store property = "XXX"
- ...
- Static Var computed property name: property data type {
- get {
- Return computed property value
- }
- Set (new property value) {
- ...
- }
- }
- }
- Enum enum Name {///define enumeration in which an instance store property cannot be defined, but you can define a static storage property, or you can define a static computed property
- static VAR (or let) store property = "XXX"
- ...
- Static Var computed property name: property data type {
- get {
- Return computed property value
- }
- Set (new property value) {
- ...
- }
- }
- }
- Class class Name {//define classes in which you can define not only instance store properties, but also static storage properties
- Staticvar (or let) store property = "XXX"
- ...
- Class (or Static) var computed property name: property data type {
- get {
- Return computed property value
- }
- Set (new property value) {
- ...
- }
- }
- }
struct static computed properties can also be read-only, with the following syntax:
- Static Var computed property name: property data type {
- Return computed property value
- }
Look at an account struct static Property Example:
)
swift-Static Properties-Standby