Type properties in Swift (static variables)

Source: Internet
Author: User

http://blog.haohtml.com/archives/15098

Type properties in Swift (static variables)Posted on 2014/06/13Type attribute syntax

In C or objective-c, static constants and static variables are defined by a specific type plus a global keyword. In the Swift programming language, a type attribute is written as part of a type definition in curly braces of the outermost type, so its scope is within the range supported by the type.

Use keywords static to define type attributes for value types, and keywords class to define type properties for class. The following example shows the syntax for a stored-type and computed-type property:

struct SomeStructure {    static var storedTypeProperty = "Some value."    static var computedTypeProperty: Int {    // 这里返回一个 Int 值    }}enum SomeEnumeration {    static var storedTypeProperty = "Some value."    static var computedTypeProperty: Int {    // 这里返回一个 Int 值    }}class SomeClass {    class var computedTypeProperty: Int {    // 这里返回一个 Int 值    }}

Attention:

The computed Type property in the example is read-only, but you can also define a readable and writable computed type property, similar to the syntax for an instance-computed property.

Gets and sets the value of the Type property

Like the properties of an instance, access to a Type property is done through the dot operator, but the Type property is obtained and set by the type itself, not by the instance. Like what:

println(SomeClass.computedTypeProperty)// 输出 "42"println(SomeStructure.storedTypeProperty)// 输出 "Some value."SomeStructure.storedTypeProperty = "Another value."println(SomeStructure.storedTypeProperty)// 输出 "Another value.”

The following example defines a struct that uses two storage type attributes to represent the sound level values of multiple channels, with an integer between 0 and 10 representing the sound level value for each channel.

The following diagram shows how to use two channels together to represent a stereo sound level value. When the level value of the channel is 0, no light is lit; when the channel level is 10, all lights are lit. In this figure, the left channel level is 9 and the right channel level is 7.

The channel model described above uses a AudioChannel struct to represent:

struct AudioChannel {    static let thresholdLevel = 10    static var maxInputLevelForAllChannels = 0    var currentLevel: Int = 0 {    didSet {        if currentLevel > AudioChannel.thresholdLevel {            // 将新电平值设置为阀值            currentLevel = AudioChannel.thresholdLevel        }        if currentLevel > AudioChannel.maxInputLevelForAllChannels {            // 存储当前电平值作为新的最大输入电平            AudioChannel.maxInputLevelForAllChannels = currentLevel        }    }    }}

AudioChannelthe structure defines 2 storage type properties to achieve these functions. The first is the thresholdLevel maximum threshold for the sound level, which is a constant with a value of 10, visible to all instances, and a maximum upper value of 10 if the sound level is above 10 (described later).

The second type attribute is a variable-stored property maxInputLevelForAllChannels , which is used to represent the AudioChannel maximum value of the level value of all instances, with an initial value of 0.

AudioChannelA named instance Store property is also defined that currentLevel represents the current level value of the channel, with a value of 0 to 10.

The property contains a property currentLevel didSet Monitor to check the value of the property after each new setting, with the following two checks:

    • If currentLevel the new value is greater than the allowable threshold thresholdLevel , the property monitor currentLevel limits the value to a threshold value thresholdLevel .
    • If the corrected currentLevel value is greater than any previous AudioChannel value in any instance, the property monitor saves the new value in a static property maxInputLevelForAllChannels .

Attention:

The didSet property monitor will be set to a different value during the first check, currentLevel but the property monitor will not be called again.

You can use AudioChannel a struct to create two channels that represent a stereo system leftChannel and rightChannel :

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

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

leftChannel.currentLevel = 7println(leftChannel.currentLevel)// 输出 "7"println(AudioChannel.maxInputLevelForAllChannels)// 输出 "7"

If you attempt to set the level of the right channel to 11, the right channel is currentLevel corrected to the maximum value of 10, and maxInputLevelForAllChannels the value is updated to 10:

rightChannel.currentLevel = 11println(rightChannel.currentLevel)// 输出 "10"println(AudioChannel.maxInputLevelForAllChannels)// 输出 "10"

Transferred from: http://numbbbbb.github.io/the-swift-programming-language-in-chinese/chapter2/10_Properties.html

This entry is posted in program developed and tagged swift by admin. Bookmark the permalink.

Type properties in Swift (static variables)

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.