Welcome to Swift (Apple's official Swift document translation and annotations V)---29~34 page

Source: Internet
Author: User

In Swift, a member variable (property) of a class does not need to be evaluated, but wants to execute a piece of code before (or after) assigning it a new value, which can be handled using Willset and didset. For example, the following code sample, the side length of a triangle (triangle) is always associated with a quad ( Square) is the same length as the edge.

Class Triangleandsquare {

var Triangle:equilateraltriangle {

Willset {

Square.sidelength = Newvalue.sidelength

}

}

var Square:square {

Willset {

Triangle.sidelength = Newvalue.sidelength

}

}

Init (size:double, name:string) {

Square = Square (sidelength:size, Name:name)

Triangle = Equilateraltriangle (sidelength:size, Name:name)

}

}

var triangleandsquare = Triangleandsquare (size:10, Name: "Another Test shape")

TriangleAndSquare.square.sideLength

TriangleAndSquare.triangle.sideLength

Triangleandsquare.square = Square (sidelength:50, name: "Larger square")

TriangleAndSquare.triangle.sideLength

The methods in the class are significantly different from those in swift. In a function, the parameter name can only be used inside the function. But in the method of the class, you can use the parameter name (in addition to the first argument of the method) when calling this method. By default, the method is called with a parameter that has the same name as the method. Of course, you can also define another name to use.

Class Counter {

var count:int = 0

Func IncrementBy (Amount:int, Numberoftimes times:int) {

Count + = Amount * times

}

}

var counter = counter ()

Counter.incrementby (2, Numberoftimes:7)

When working with option values, you can use the? operation (just like the method/property/index tag). If the value before the? is empty (nil), then all of it is ignored, and the value of the entire expression is empty (nil). If the previous value is not empty (nil), then the value of the option will be based on? Subsequent execution results are determined. In both cases, the value of the entire expression is as an option value.

Let Optionalsquare:square? = Square (sidelength:2.5, name: "Optional Square")

Let Sidelength = Optionalsquare?. Sidelength

Using enum in Swift to create enum types, enumerations are the same as classes and other types, and can have their own methods.

Enum Rank:int {

Case ACE = 1

Case A, three, four, Five, Six, Seven, Eight, Nine, Ten

Case Jack, Queen, King.

Func simpledescription (), String {

Switch Self {

Case. Ace:

Return "Ace"

Case. Jack:

Return "Jack"

Case. Queen:

Return "Queen"

Case. King:

Return "King"

Default

Return String (Self.toraw ())

}

}

}

Let Ace = Rank.ace

Let Acerawvalue = Ace.toraw ()

Experiment//Exercises

Write A function that compares the Rank values by comparing their raw values.

Write a function to compare two rank values (by comparing their initial values).

In the above example, the initial value type of the enumeration is int, so you only need to specify the first initial value, followed by a sequential assignment. You can also use a string or floating-point value as the initial value type of the enumeration.

You can convert between the initial value and the enumeration value by using the Toraw and Fromraw methods.

If Let Convertedrank = Rank.fromraw (3) {

Let threedescription = Convertedrank.simpledescription ()

}

The value of an enumeration member is an exact actual value and does not provide another way to assign an initial value. In fact, if it's really a meaningless initial value, you don't have to assign a value.

Enum Suit {

Case Spades, Hearts, Diamonds, Clubs

Func simpledescription (), String {

Switch Self {

Case. Spades:

Return "Spades"

Case. Hearts:

Return "Hearts"

Case. Diamonds:

Return "Diamonds"

Case. Clubs:

Return "clubs"

}

}

}

Let hearts = Suit.hearts

Let heartsdescription = Hearts.simpledescription ()

Experiment//Exercises

Add a color method to Suit this returns "Black" for spades and clubs, and returns ' red ' for Hearts and diamonds.

Add a color method to suit, if it is a spade/plum, the return value is black, if it is hearts and squares, the return value is red

Note that the enumeration member Hearts is used in the above code in two places: The enumeration assigns a value to the hearts constant, Hearts identifies the constant (because no obvious type is specified), and in the switch statement, by abbreviation. Hearts uses an enumeration (since self is already a suit). You can use abbreviations if the type of the value is clear.

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.