The Swift property associates a value with a specific class, structure, or enumeration.
Attributes can be divided into storage attributes and calculation attributes:
Storage attribute | Calculation attribute | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
Store constants or variables as part of an instance | Calculate (rather than store) a value | ||||||||||
For classes and structures | For classes, structures, and enumerations Storage and calculation properties are typically used for specific types of instances. Properties can also be used directly for the type itself, which is called a type property. In addition, a property watcher can be defined to monitor changes in property values to trigger a custom action. The property watcher can be added to a storage property written by itself or to a property inherited from the parent class. Simply put, a storage property is a constant or variable stored in an instance of a particular class or structure. A storage property can be a variable storage property (with keywords You can specify default values when defining storage properties You can also set or modify the value of a storage property during construction, or even modify the value of a constant storage property The output of the above program execution is as follows: Consider the following code: The pi in the code specifies the default value (pi = 3.1415) when defining the storage property, so whenever you instantiate the structure, it will notchange. If you define a constant storage property, you will get an error if you try to modify it, as shown below: The execution of the above procedures will report an error as follows: Numbers’ is a constant and you can’t modify it. A deferred storage property is a property whose initial value is calculated only when it is called for the first time. Use before the property declaration Note: the deferred storage property must be declared as a variable using the``var`` keyword, because the value of the property may not be available until the instance construction is complete. A constant property must have an initial value before the construction process is complete, so it cannot be declared as a delay property. Latency storage properties are generally used to: Delay the creation of the object. When the value of an attribute depends on other unknown classes The output of the above program execution is as follows: If you have any experience with Objective-C, you should know that Objective-C provides two ways to store values and references for class instances. For properties, you can also use instance variables as the back-end storage of property values. These theories are uniformly implemented by attributes in the Swift programming language. Properties in Swift have no corresponding instance variables, and the back-end storage of properties cannot be accessed directly. This avoids the trouble of access methods in different scenarios, and simplifies the definition of attributes into a single statement. All information about attributes in a type– including naming, type, and memory management features– is defined in a unique place (in the type definition). In addition to storing properties, classes, structures, and enumerations candefine calculated properties, which do not store values directly, but provide a The output of the above program execution is as follows: If you calculate the property’s Only The read-only calculation property always returns a value, which can be passed through the dot (.) Operator, but you cannot set a new value. The output of the above program execution is as follows: Note: Must be used The property watcher monitors and responds to changes in property values, calling the property watcher every time the property is set, even when the new value is the same as the current value. You can add property watchers for storage properties other than deferred storage properties, or you can add property observers for inherited properties, including storage and calculation properties, by overloading properties. Note: there is no need to add a property watcher for calculated properties that cannot be overloaded, because you can use the You can add one or all of the following observers to the property: The output of the above program execution is as follows: The patterns described by the calculated properties and property watchers can also be used for global and local variables. Local variable Global variable A variable defined within a function, method, or closure. Functions, methods, closures, or variables defined outside of any type. Used to store and retrieve values. Used to store and retrieve values. Storage properties are used to get and set values. Storage properties are used to get and set values. It is also used to calculate attributes. It is also used to calculate attributes. Type properties are written in the outermost curly braces ({}) of the type as part of the type definition. Use keywords Note: the computed type properties in the example are read-only, but you canalso define readable and writable computed type properties, similar to the syntax of the instance evaluation properties. Similar to the properties of an instance, type properties are also accessed through the dot operator (.) Let’s do it. However, type properties are obtained and set by the type itself, not by the instance. Examples are as follows: The output of the above program execution is as follows: Principles, Technologies, and Methods of Geographic Information Systems 102 In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress. |