Global variables, local variables, and type attributes of swift

Source: Internet
Author: User

Global and local variables

The pattern described by the calculation property and property monitor can also be used for global variables and local variables. global variables are variables defined outside functions, methods, closures, or any type, local variables are defined within functions, methods, or closures.

 

The global or local variables mentioned in the previous section are stored variables. Similar to the storage attributes, they provide specific types of storage space and allow reading and writing.

 

In addition, computing variables can be defined globally or locally and the monitor can be defined for storage variables. Computing variables return a calculated value, not a stored value, just like the computing attribute, the Declaration format is the same.

 

Note:

 

Global constants or variables are computed with delay. They are similar to delayed storage properties. The difference is that global constants or variables do not need to be marked with the @ lazy feature.

 

Constants or variables in the local range do not delay calculation.

 

Type property

The attributes of an instance belong to a specific type. Each time a type is instantiated, it has its own set of attribute values. The attributes of an instance are independent of each other.

 

You can also define attributes for the type. No matter how many instances the type has, these attributes are unique. This type of attribute is a type attribute.

 

Type attributes are used to define the data shared by all instances of a specific type. For example, a constant that can be used by all instances (just like a static constant in C ), or a variable that can be accessed by all instances (just like static variables in C ).

 

For value types (struct and enumeration), you can define storage and computing type attributes. For classes, you can only define computing type attributes.

 

The storage type attribute of the value type can be a variable or constant. The computing type attribute is defined as a variable attribute like the computing property of the instance.

 

Note:

 

Different from the storage properties of an instance, you must specify the default value for the storage type attribute, because the type itself cannot be used to assign values to the type attribute during initialization.

 

Type attribute syntax

In C or objective-C, static constants and static variables are defined by adding the Global keyword to a specific type. In swift programming language, a type attribute is written in curly brackets on the outermost layer of a type as part of a type definition, so its scope of action is within the range supported by the type.

 

Use the keyword static to define the type attribute of the value type, and the keyword class to define the type attribute for the class. The following example demonstrates the syntax of the storage and computing type attributes:

 

Struct somestructure {static var storedtypeproperty = "some value. "Static Var computedtypeproperty: int {// here an int value is returned} Enum someenumeration {static var storedtypeproperty =" some value. "Static Var computedtypeproperty: int {// here an int value is returned} class someclass {class var computedtypeproperty: int {// here an int value is returned }}


Note:

 

The computing type attribute in this example is read-only, but can also define readable and writable computing type attributes, similar to the syntax of instance computing properties.

 

Get and set the value of the type attribute

Like instance attributes, access to type attributes is also carried out through the dot operator. However, type attributes are obtained and set through the type itself, rather than through the instance. For example:

 

Println (someclass. computedtypeproperty) // output "42" println (somestructure. storedtypeproperty) // outputs "somevalue. "somestructure. storedtypeproperty = "another value. "println (somestructure. storedtypeproperty) // output "anothervalue."


The following example defines a struct that uses two storage type attributes to represent the sound level value of multiple channels. Each channel has an integer between 0 and 10 to indicate the sound level value.

 

The following figure shows how two sound channels are combined to represent the sound level of a stereo. When the channel level is 0, no lamp is on; when the channel level is 10, all lights are on. In this figure, the level of the left audio channel is 9, and the level of the right audio channel is 7.

 

 

 

The channel model described above uses the audiochannel struct to represent it:

 

Struct audiochannel {static let thresholdlevel = 10 Static Var maxinputlevelforallchannels = 0 var currentlevel: Int = 0 {didset {If currentlevel> audiochannel. thresholdlevel {// set the new electric flat value to the threshold value currentlevel = audiochannel. thresholdlevel} If currentlevel> audiochannel. maxinputlevelforallchannels {// store the current level value as the new maximum input level audiochannel. maxinputlevelforallchannels = currentlevel }}}}


The structure audiochannel defines two storage type attributes to implement the above functions. The first is thresholdlevel, which indicates the maximum threshold of the sound level. It is a constant with a value of 10 and is visible to all instances. If the sound level is higher than 10, the maximum value is 10 (see the description below ).

 

The second type attribute is the storage type attribute maxinputlevelforallchannels, which indicates the maximum value of the level value of all audiochannel instances. The initial value is 0.

 

Audiochannel also defines an Instance Storage attribute named currentlevel, indicating the current level value of the current channel. The value ranges from 0 to 10.

 

The property currentlevel contains the didset property monitor to check the attribute values after each new setting. There are two checks:

 

If the new value of currentlevel is greater than the allowed threshold value thresholdlevel, property monitor limits the value of currentlevel to the threshold value thresholdlevel.

If the corrected currentlevel value is greater than any previous audiochannel value, property monitor stores the new value in the static attribute maxinputlevelforallchannels.

Note:

 

During the first check, the didset property monitor sets currentlevel to a different value, but does not call the property monitor again.

You can use the structure audiochannel to create two channels leftchannel and rightchannel representing the stereo system:

 

var leftChannel = AudioChannel()var rightChannel = AudioChannel()


If you set the level of the left channel to 7, the type attribute maxinputlevelforallchannels is also updated to 7:

 

Leftchannel. currentlevel = 7 println (leftchannel. currentlevel) // output "7" println (audiochannel. maxinputlevelforallchannels) // output "7". If you try to set the right channel level to 11, the currentlevel of the right channel is corrected to the maximum value of 10, and the value of maxinputlevelforallchannels is also updated to 10: rightchannel. currentlevel = 11 println (rightchannel. currentlevel) // output "10" println (audiochannel. maxinputlevelforallchannels) // output "10"


Global variables, local variables, and type attributes of swift

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.