iOS development Language Swift Entry---properties

Source: Internet
Author: User
<span id="Label3"></p>iOS development language Swift Getting started Serial-properties<p><p>Property associates a value with a specific class, struct, or Enumeration. A store property stores a constant or variable as part of an instance, and computes a value for the property calculation instead of storing it. Computed properties can be used in classes, structs, and enumerations, and stored properties are used only for classes and struct Bodies.<br>Storage properties and computed properties are typically used for instances of a particular type, but properties can also be used directly on the type itself, which is called a type Property.<br>In addition, you can define a property monitor to monitor changes in property values to trigger a custom Action. The property monitor can be added to a stored property that you write, or to a property that inherits from a parent class.<br>  </p></p>Store Properties<p><p>Simply put, a stored property is a constant or variable stored in an instance of a particular class or struct, which can be a variable store property (defined with the keyword Var) or a constant storage attribute (defined by the keyword let).<br>You can specify a default value when defining storage properties, refer to the default property values section of the construction procedure Chapter. You can also set or modify the value of a stored property during construction, or even modify the value of a constant store property, refer to the section on modifying a constant storage property in the initialization phase of the construction process Chapter.<br>The following example defines a struct named fixedlengthrange, which describes an interval in which the range width cannot be modified after creation:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">struct</span> FixedLengthRange { <span class="hljs-keyword">var</span> firstValue: Int <span class="hljs-keyword">let</span> length: Int}<span class="hljs-keyword">var</span><span class="hljs-number">0</span><span class="hljs-number">3</span>)<span class="hljs-comment">// 该区间表示整数0,1,2</span><span class="hljs-number">6</span><span class="hljs-comment">// 该区间现在表示整数6,7,8</span></code></pre></pre><p><p>An instance of Fixedlengthrange contains a variable store property named Firstvalue and a constant storage property named Length.  In the example above, length is assigned when the instance is created, because it is a constant storage property, so its value cannot be modified after that. Constants and Storage properties<br>If you create an instance of a struct and assign a value to a constant, you cannot modify any of the properties of the instance, even if the variable store property is Defined:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">let</span><span class="hljs-number">0</span><span class="hljs-number">4</span>)<span class="hljs-comment">// 该区间表示整数0,1,2,3</span><span class="hljs-number">6</span><span class="hljs-comment">// 尽管 firstValue 是个变量属性,这里还是会报错</span></code></pre></pre><p><p>Because Rangeoffouritems is declared as a constant (with the Let keyword), even if Firstvalue is a variable property, it can no longer be modified.<br>This behavior is because the struct (struct) belongs to a value type. When an instance of a value type is declared as a constant, all its properties are also constants.<br>Classes that belong to reference types are different, and you can still modify the Instance's variable properties after assigning an instance of a reference type to a Constant.</p></p>Deferred storage properties<p><p>  <br>A deferred store property is a property whose initial value is evaluated when it is first Called. Use @lazy to indicate a deferred storage property before the property Declaration.<br>Attention:<br>You must declare the deferred storage property as a variable (using the var keyword), because the value of the property may not be available until the instance construction is Complete. The constant property must have an initial value before the construction process is complete, and therefore cannot be declared as a deferred Attribute.<br>Deferred properties are useful when the value of a property depends on an external factor that cannot be known before the end of the Instance's construction process, or when the value of the property requires complex or large computations, it can be evaluated only when Needed.<br>The following example uses deferred storage properties to avoid unnecessary initialization of complex classes. The Dataimporter and DataManager two classes are defined in the example, and the following is part of the code:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="hljs scala"><span class="hljs-class"><span class="hljs-keyword">class </span> <span class="hljs-title">dataimporter </span> {</span> <span class="hljs-comment">/* Dataimporter is a class that imports data from external Files. Initialization of this class can take a lot of time. */</span> <span class="hljs-keyword">var </span> fileName = <span class="hljs-string"> "data.txt" </span> <span class=" Hljs-comment ">//this is a data import feature </span>}<span class=" hljs-class "><span class=" hljs-keyword ">class </span> <span class=" Hljs-title ">datamanager </span> {</span> <span class=" hljs-annotation "> @lazy </span> <span class=" Hljs-keyword ">var </span> importer = Dataimporter () <span class="hljs-keyword">var </span> data = string[] () <span class="hljs-comment">//this is to provide data management functionality </span>}let Manager = DataManager () Manager.data + = <span class="hljs-string"> "Some data" </span> Manager.data + = <span class="hljs-string"> "Some more data" </span> <span class="hljs-comment">//dataimporter Instance The Importer property has not been created </span> </code> </pre></pre><p><p>The DataManager class contains a storage property named data, and the initial value is an empty string array. Although not all of the code is written, the purpose of the DataManager class is to manage and provide access to the array of strings.<br>One feature of DataManager is the ability to import data from a file, which is provided by the Dataimporter class, and Dataimporter takes time to complete initialization: because its instance might open a file at initialization time, It also reads the contents of the file into Memory.<br>DataManager may also not import data from the File. So when an instance of DataManager is created, it is not necessary to create an instance of datamanager, and it is wiser to create it when Dataimporter is Used.<br>Because @lazy is used, the Importer property is created only when it is first Accessed. For example, when accessing its properties filename:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs scss"><span class="hljs-function">println(manager.importer.fileName)</span><span class="hljs-comment">// DataImporter 实例的 importer 属性现在被创建了</span><span class="hljs-comment">// 输出 "data.txt”</span></code></pre></pre>Storing properties and instance variables<p><p>   <br>If you have had objective-c experience, you should know that there are two ways to store values and references in class Instances. For properties, You can also use instance variables as Back-end storage for property Values.<br>These theories are implemented uniformly using attributes in programming Languages. The properties in Swift do not have corresponding instance variables, and the backend storage of the properties is not directly accessible. This avoids the problem of access patterns in different scenarios, and simplifies the definition of attributes to a single statement. All information about a property in a type-including naming, type, and memory management Characteristics-is defined in the only place (in the type definition).<br>Calculated properties<br>In addition to storing properties, classes, structs, and enumerations can define computed properties, calculated properties do not store values directly, but instead provide a getter to get the value, an optional setter to indirectly set the value of another property or Variable.</p></p><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword"><span class="hljs-keyword">struct</span></span>Point {<span class="hljs-keyword"><span class="hljs-keyword">var</span></span>x =<span class="hljs-number"><span class="hljs-number">0.0</span></span>, y =<span class="hljs-number"><span class="hljs-number">0.0</span></span>}<span class="hljs-keyword"><span class="hljs-keyword">struct</span></span>Size {<span class="hljs-keyword"><span class="hljs-keyword">var</span></span>width =<span class="hljs-number"><span class="hljs-number">0.0</span></span>, height =<span class="hljs-number"><span class="hljs-number">0.0</span></span>}<span class="hljs-keyword"><span class="hljs-keyword">struct</span></span>Rect {<span class="hljs-keyword"><span class="hljs-keyword">var</span></span>Origin = Point ()<span class="hljs-keyword"><span class="hljs-keyword">var</span></span>size = Size ()<span class="hljs-keyword"><span class="hljs-keyword">var</span></span>Center:point {<span class="hljs-keyword"><span class="hljs-keyword">Get</span></span>{<span class="hljs-keyword"><span class="hljs-keyword"></span> let</span>CenterX = origin.x + (size.width/<span class="hljs-number"><span class="hljs-number">2</span></span>)<span class="hljs-keyword"><span class="hljs-keyword"></span> let</span>CenterY = origin.y + (size.height/<span class="hljs-number"><span class="hljs-number">2</span></span>)<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>Point (x:centerx, y:centery)}<span class="hljs-keyword"><span class="hljs-keyword">Set</span></span>(newcenter) {origin.x = newcenter.x-(size.width/<span class="hljs-number"><span class="hljs-number">2</span></span>) ORIGIN.Y = newcenter.y-(size.height/<span class="hljs-number"><span class="hljs-number">2</span></span>) } }}<span class="hljs-keyword"><span class="hljs-keyword">var</span></span>Square = Rect (origin:point (x:<span class="hljs-number"><span class="hljs-number">0.0</span></span>Y:<span class="hljs-number"><span class="hljs-number">0.0</span></span>), size:size (width:<span class="hljs-number"><span class="hljs-number">10.0</span></span>, height:<span class="hljs-number"><span class="hljs-number">10.0</span></span>))<span class="hljs-keyword"><span class="hljs-keyword"></span> let</span>Initialsquarecenter = Square.centersquare.center = Point (x:<span class="hljs-number"><span class="hljs-number">15.0</span></span>Y:<span class="hljs-number"><span class="hljs-number">15.0</span></span>) println (<span class="hljs-string"><span class="hljs-string">"square.origin is now at (\ (square.origin.x), \ (square.origin.y))"</span></span>)<span class="hljs-comment"><span class="hljs-comment">//output "square.origin is today at (10.0, 10.0)"</span></span></code></pre><p><p>This example defines the structure of 3 geometric shapes:<br>The point encapsulates a coordinate (x, y)<br>The Size encapsulates a width and height<br>Rect represents a rectangle with an origin and size<br>Rect also provides a computed property called Center. The center point of a rectangle can be calculated from the origin and dimension, so it is not necessary to save it as an explicitly declared Point. The computed properties of Rect Center provides a custom getter and setter to get and set the center point of the rectangle as if it had a stored Property.<br>The example next creates a rect instance named square, where the initial value is (0, 0) and the width height is 10. Blue Squares.<br>The center property of Square can be accessed through the dot operator (square.center), which invokes the getter to get the value of the Property. Instead of returning a value that already exists directly, the getter actually calculates and returns a new point to represent the center of the Square. As the code shows, it correctly returns the center point (5, 5).<br>The Center property is then set to a new value (15, 15), which represents the position of moving the square to the Orange Square to the Right. Setting the value of the property center invokes the setter to modify the values of the X and Y properties of origin, thereby moving the square to a new Position.</p></p><p><p></p></p>Convenient setter Declaration<p><p>If the setter for the computed property does not define a parameter name that represents the new value, you can use the default name Newvalue. The following is a rect struct code that uses the handy setter declaration:</p></p><pre class="prettyprint"><code class=" hljs avrasm">struct Alternativerect {var origin = Point () var size = size () var center:point {get {let CenterX = Origin<span class="hljs-preprocessor"><span class="hljs-preprocessor">. x</span></span>+ (size<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Width</span></span>/<span class="hljs-number"><span class="hljs-number">2</span></span>) Let CenterY = Origin<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Y</span></span>+ (size<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Height</span></span>/<span class="hljs-number"><span class="hljs-number">2</span></span>) Return Point (<span class="hljs-built_in"><span class="hljs-built_in">x</span></span>: centerx,<span class="hljs-built_in"><span class="hljs-built_in">y</span></span>: centery)}<span class="hljs-keyword"><span class="hljs-keyword">Set</span></span>{origin<span class="hljs-preprocessor"><span class="hljs-preprocessor">. x</span></span>= NewValue<span class="hljs-preprocessor"><span class="hljs-preprocessor">. x</span></span>-(size<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Width</span></span>/<span class="hljs-number"><span class="hljs-number">2</span></span>) origin<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Y</span></span>= NewValue<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Y</span></span>-(size<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Height</span></span>/<span class="hljs-number"><span class="hljs-number">2</span></span>) } }}</code></pre>Read-only Computed Properties<p><p>Only a getter without a setter is a computed property that is a read-only computed Property. A read-only computed property always returns a value that can be accessed through the point operator, but cannot set a new Value.<br>Attention:<br>You must use the var keyword to define calculated properties, including Read-only computed properties, because their values are not fixed. The var keyword is used only to declare a constant property, which represents a value that cannot be modified after Initialization.<br>Attention:<br>You must use the var keyword to define calculated properties, including Read-only computed properties, because their values are not fixed. The var keyword is used only to declare a constant property, which represents a value that cannot be modified after Initialization.</p></p><p><p>The declaration of a read-only computed property can be stripped of the Get keyword and braces:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="hljs cs"><span class="hljs-keyword">struct </span> Cuboid {<span class=" Hljs-keyword ">var </span> width = <span class=" hljs-number ">0.0 </span>, height = <span class=" hljs-number ">0.0 </span> , depth = <span class="hljs-number">0.0 </span> <span class="hljs-keyword">var </span> volume:double {<span class="h Ljs-keyword ">return </span> Width * Height * depth}}<span class=" hljs-keyword ">let </span> fourbyfivebytwo = Cuboid (widt H: <span class="hljs-number">4.0 </span>, height: <span class="hljs-number">5.0 </span>, depth: <span class="hljs-number">2.0 </span>) println (<span class="hljs-string"> "the volume of fourbyfivebytwo is \ (fourbyfivebytwo.volume)" </span>) <span class=" Hljs-comment ">//output" The volume of Fourbyfivebytwo is 40.0 "</span> </code> </pre></pre><p><p>This example defines a struct named cuboid, a cube that represents a three-dimensional space, contains the width, height, and depth properties, and a read-only computed property named volume to return the volume of the Cube. Setting the value of the volume is meaningless, as the volume can be calculated by width, height, and Depth. however, It is useful for Cuboid to provide a read-only computed property to allow external users to get the volume directly.<br>  </p></p>Property Monitor<p><p>The property monitor monitors and responds to changes in the property values, and the property monitor is called every time the property is set, even if the new value is the same as the current Value.<br>You can add property monitors for storage properties other than deferred storage properties, or you can add property monitors for inherited properties, including storage properties and computed properties, by overloading the Properties. For property overloading, refer to the overloads of the inheritance Chapter.<br>Attention:<br>You do not need to add property monitors for computed properties that cannot be overloaded, because you can monitor and respond to changes in values directly through the Setter.<br>You can add one or all of the following monitors for a property:<br>Willset called before setting a new value<br>Didset called immediately after the new value has been set<br>The Willset monitor will pass in the new property value as a fixed parameter, you can specify a name for this parameter in the implementation code of the willset, and if you do not specify the parameter is still available, the default name NewValue is Used.<br>similarly, The Didset monitor passes the old property values as parameters, either by naming the parameter or by using the default parameter name Oldvalue.</p></p><p><p>Attention:<br>Willset and Didset monitors are not called during property initialization, they are called only when the value of the property is set outside of the Initialization.<br>Attention:<br>Willset and Didset monitors are not called during property initialization, they are called only when the value of the property is set outside of the Initialization.<br>Here is a practical example of Willset and didset, which defines a class called stepcounter, which is used to count the total number of steps when a person is walking, and can be used in conjunction with the input data of a pedometer or other routine exercise statistic device.</p></p><pre class="prettyprint"><code class=" hljs javascript"><span class="hljs-keyword"><span class="hljs-keyword">class</span></span>Stepcounter {<span class="hljs-keyword"><span class="hljs-keyword">var</span></span>Totalsteps:int =<span class="hljs-number"><span class="hljs-number">0</span></span>{willset (newtotalsteps) {println (<span class="hljs-string"><span class="hljs-string">"about-to-set totalsteps to \ (newtotalsteps)"</span></span>)} Didset {<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>Totalsteps > OldValue {println (<span class="hljs-string"><span class="hljs-string">"Added \ (totalsteps-oldvalue) steps"</span></span>) } } }}<span class="hljs-keyword"><span class="hljs-keyword"></span> let</span>Stepcounter = stepcounter () stepcounter.totalsteps =<span class="hljs-number"><span class="hljs-number"></span> $</span><span class="hljs-comment"><span class="hljs-comment">//about to set Totalsteps</span> to</span><span class="hljs-comment"><span class="hljs-comment">//Added Steps</span></span>Stepcounter.totalsteps =<span class="hljs-number"><span class="hljs-number"></span> the</span><span class="hljs-comment"><span class="hljs-comment">//about to set Totalsteps</span> to</span><span class="hljs-comment"><span class="hljs-comment">//Added Steps</span></span>Stepcounter.totalsteps =<span class="hljs-number"><span class="hljs-number">896</span></span><span class="hljs-comment"><span class="hljs-comment">//about to set Totalsteps to 896</span></span><span class="hljs-comment"><span class="hljs-comment">//Added 536 Steps</span></span></code></pre><p><p>The Stepcounter class defines an int type property totalsteps, which is a storage property that contains Willset and Didset Monitors.<br>When Totalsteps sets a new value, its willset and didset monitors are called, even when the new value is exactly the same as the current Value.<br>The Willset monitor in the example customizes the parameter representing the new value to newtotalsteps, which simply outputs the new Value.<br>The Didset Monitor is called after the value of Totalsteps is changed, comparing the new value to the old value, and if the total number of steps increases, the output of a message indicates how many steps are Added. Didset does not provide a custom name, so the default value oldvalue represents the parameter name of the old Value.<br>Attention:<br>If you assign a value to a property in the Didset monitor, this value replaces the value set before the Monitor.<br>  </p></p>Global variables and local variables<p><p>The schemas described by the computed properties and property monitors can also be used for global variables and local variables, which are variables defined outside of a function, method, closure, or any type, and local variables are variables defined inside a function, method, or closure.<br>The global or local variables mentioned in the previous section are stored variables, similar to stored properties, which provide a specific type of storage space and allow read and Write.<br>In addition, both global and local scopes can define computed variables and define monitors for Stored-type variables, which, like computed properties, return a computed value instead of a stored value, and the declaration format is exactly the Same.<br>Attention:<br>Global constants or variables are deferred, similar to deferred storage properties, except that global constants or variables do not need to be tagged with the @lazy attribute.<br>Local-scope constants or variables do not delay calculations.<br>  </p></p>Type property<p><p>The properties of an instance belong to a particular type instance, each of which has its own set of property values each time the type is instantiated, and the attributes between the instances are independent of each other.<br>You can also define properties for the type itself, which have only one copy, regardless of the number of instances of the Type. This property is the type Attribute.<br>Type properties are used to define data shared by all instances of a particular type, such as a constant that all instances can use (just like a static constant in c), or a variable that all instances can access (just like a static variable in the C language).<br>For value types (refers to structs and Enumerations) you can define both the storage type and the computed type property, and for the class, only the computed type Property.<br>A stored Type property of a value type can be a variable or constant, and a computed type property is defined as a variable property as the computed property of an Instance.<br>Attention:<br>Unlike the storage properties of an instance, you must specify a default value for the stored type property, because the type itself cannot use the constructor to assign a value to the Type property during Initialization.<br> </p></p>Type attribute syntax<p><p>In C or objective-c, static constants and static variables are defined by a specific type plus the 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.<br>Use the keyword static to define the Type property of a value type, and the keyword class to define the Type property for the class. The following example shows the syntax for a stored-type and Computed-type property:</p></p><pre class="prettyprint"><code class=" hljs vala"><span class="hljs-keyword"><span class="hljs-keyword">struct</span></span>somestructure {<span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">var</span></span>Storedtypeproperty =<span class="hljs-string"><span class="hljs-string">"Some value."</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">var</span></span>Computedtypeproperty:int {<span class="hljs-comment"><span class="hljs-comment">//returns an Int value here</span></span>}}<span class="hljs-keyword"><span class="hljs-keyword">enum</span></span>someenumeration {<span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">var</span></span>Storedtypeproperty =<span class="hljs-string"><span class="hljs-string">"Some value."</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">var</span></span>Computedtypeproperty:int {<span class="hljs-comment"><span class="hljs-comment">//returns an Int value here</span></span>}}<span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">someclass</span> {</span></span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">var</span> <span class="hljs-title">computedtypeproperty</span>: <span class="hljs-title">Int</span> {</span></span> <span class="hljs-comment"><span class="hljs-comment">//returns an Int value here</span></span>}}</code></pre><p><p>Attention:<br>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.<br>Gets and sets the value of the Type property<br>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:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs avrasm">println(SomeClass<span class="hljs-preprocessor">.computedTypeProperty</span><span class="hljs-string">"42"</span>println(SomeStructure<span class="hljs-preprocessor">.storedTypeProperty</span><span class="hljs-string">"Some value."</span>SomeStructure<span class="hljs-preprocessor">.storedTypeProperty</span><span class="hljs-string">"Another value."</span>println(SomeStructure<span class="hljs-preprocessor">.storedTypeProperty</span><span class="hljs-string">"Another value.”</span></code></pre></pre><p><p>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.<br>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.</p></p><p><p></p></p><p><p>The channel model described above uses the Audiochannel structure to represent:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="hljs cs"><span class="hljs-keyword">struct </span> audiochannel {<span class="hljs-keyword">static </span> <span class="hljs-keyword">let </span> thresholdlevel = <span class="hljs-number">10 </span> <span class="hljs-keyword">static </span> <span class="hljs-keyword">var </span> maxinputlevelforallchannels = <span class="hljs-number">0 </span> <span class="hljs-keyword">var </span> Currentlevel:int = <span class="hljs-number">0 </span> {didset {<span class="hljs-keyword">if </span> currentle Vel > Audiochannel.thresholdlevel {<span class="hljs-comment">//set The new level value to threshold </span> CurrentLevel = audiochannel.thresholdlevel} <span class="hljs-keyword">if </span> currentlevel > Audiochannel.maxinput Levelforallchannels {<span class="hljs-comment">//stores The current level value as the new maximum input level </span> Audiochannel.maxinputle Velforallchannels = currentlevel}}} </code> </pre></pre><p><p>The structure Audiochannel defines 2 storage type attributes to achieve these Functions. The first is thresholdlevel, which represents the maximum upper threshold of the sound level, which is a constant value of 10, visible to all instances, and a maximum upper value of 10 if the sound level is above 10 (described later).<br>The second type attribute is the Variable-stored-type attribute maxinputlevelforallchannels, which is used to represent the maximum value of the level value of all Audiochannel instances, with an initial value of 0.<br>An instance store property named CurrentLevel is also defined that represents the current level value of the channel, with a value of 0 to 10.<br>The CurrentLevel property contains the Didset property monitor to check the value of the property after each new setting, with the following two checks:<br>If the new value of CurrentLevel is greater than the allowable threshold thresholdlevel, the property monitor limits the value of CurrentLevel to the threshold thresholdlevel.<br>If the corrected CurrentLevel value is greater than any previous value in any Audiochannel instance, the property monitor saves the new value in the static property Maxinputlevelforallchannels.<br>Attention:<br>The Didset property monitor sets CurrentLevel to a different value during the first check, but the property monitor is not called Again.<br>You can use the struct Audiochannel to create two channels Leftchannel and rightchannel that represent a stereo system:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">var</span> leftChannel = AudioChannel()<span class="hljs-keyword">var</span> rightChannel = AudioChannel()</code></pre></pre><p><p>If you set the level of the left channel to 7, the type attribute maxinputlevelforallchannels will also be updated to 7:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs avrasm">leftChannel<span class="hljs-preprocessor">.currentLevel</span><span class="hljs-number">7</span>println(leftChannel<span class="hljs-preprocessor">.currentLevel</span><span class="hljs-string">"7"</span>println(AudioChannel<span class="hljs-preprocessor">.maxInputLevelForAllChannels</span><span class="hljs-string">"7"</span></code></pre></pre><p><p>If you attempt to set the level of the right channel to 11, the currentlevel of the right channel is corrected to the maximum value of 10, and the Maxinputlevelforallchannels value is updated to 10:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs avrasm">rightChannel<span class="hljs-preprocessor">.currentLevel</span><span class="hljs-number">11</span>println(rightChannel<span class="hljs-preprocessor">.currentLevel</span><span class="hljs-string">"10"</span>println(AudioChannel<span class="hljs-preprocessor">.maxInputLevelForAllChannels</span><span class="hljs-string">"10"</span></code></pre></pre> <p><p>iOS development Language Swift Entry---properties</p></p></span>

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.