Welcome to Swift (initial translation and annotations of Apple's official Swift document 11)---70~76 page (chapter II)

Source: Internet
Author: User

Type Aliases (types alias)

A type alias is another name that already exists for a type definition. Use the keyword Typealias when defining the type alias.

When you want to use a name to refer to an already existing type, the type alias is very effective and better suited to the context of the code. For example, when you want to process a data of a specified size:

Typealias audiosample = UInt16

Once you have defined a type alias, you can replace the original type name in any place:

var maxamplitudefound = Audiosample.min

Maxamplitudefound is now 0

In the above code, Audiosample is defined as the alias of UInt16, because it is an alias, so the call audiosample.min is actually called Uint16.min, which finally assigns the variable maxamplitudefound an initialized 0 .

Booleans (bool type)

Swift has a basic Boolean type: bool, which is a Boolean value used for logical judgment. Because the Bool value can only be true or false. In Swift, the system provides two constants of the Boolean type: True and false:

Let Orangesareorange = True

Let turnipsaredelicious = False

Code, the types of constants Orangesareorange and turnipsaredelicious are bool by using Boolean type values. As with the use of int and double, the above code does not after assigning a value of true or false to a constant. It is necessary to declare that they are of the Boolean type. In Swift, type-judging help code is more precise and readable, and the type of a constant or variable is the type of the value when the value of a known type is initialized to a constant or variable.

The value of a Boolean type is particularly useful when making conditional judgments, such as the IF statement in the following code:

If turnipsaredelicious {

println ("Mmm, Tasty turnips!")

} else {

println ("Eww, turnips is horrible.")

}

Prints "Eww, turnips is horrible."

The IF condition judgment statement is referred to in the Process control.

In swift, type safety prohibits the assignment of Boolean types to values of non-Boolean types. The following code will report a compiler error:

Let i = 1

If I {

This example would not compile, and would report an error

}

However, the following code is correct:

Let i = 1

if i = = 1 {

This example'll compile successfully

}

Because the result of the i = = 1 comparison is a Boolean type, it is possible to pass type safety checks.

Like other types of swift security, Boolean type detection avoids occasional errors and potential code problems.

Tuples (meta-group)

Tuples are values that combine multiple values to form a composite. The values inside the tuple can be any type, or they can be different.

In the following example, (404, "not Found") is a tuple that describes the state encoding of HTTP. HTTP status encoding is the specific value that is returned from the Web server when you make Web Access. 404, "Not Found" This status code indicates that the visited Web page does not exist.

Let Http404error = (404, "not Found")

Http404error is of type (int., String), and Equals (404, "not Found")

Tuples (404, "Not Found") assign an Int type and a String type to the HTTP status code: A numeric value and a readability description, which can also be described as: a tuple of type (int,string).

When you create tuples, you can arrange the types arbitrarily, and you can include any different types. For example, you can define a tuple of type (int,int,int), or you can define a tuple of type (String,bool), or any other type you want.

Parsing tuples, you can independently access constants or variables in tuples, as in the following code:

Let (StatusCode, statusmessage) = Http404error

println ("The Status code is \ (StatusCode)")

Prints "The Status code is 404"

println ("The status message is \ (StatusMessage)")

Prints "The status message is not Found"

If only some of the tuple's values need to be processed, the ignored portions are handled using (_).

Let (Justthestatuscode, _) = Http404error

println ("The Status code is \ (Justthestatuscode)")

Prints "The Status code is 404

You can also use an index to access the independent element values of a tuple. The tuple's index starts with 0:

println ("The Status code is \ (http404error.0)")

Prints "The Status code is 404"

println ("The status message is \ (Http404error.1)")

Prints "The status message is not Found"

Each element can be named when the tuple is defined:

Let Http200status = (statuscode:200, description: "OK")

If you name the elements, you can access their values by the name of the element:

println ("The Status code is \ (Http200status.statuscode)")

Prints "The status code is 200"

println ("The status message is \ (http200status.description)")

Prints "The status message is OK"

Tuples are useful when the return value of a function is received. A function that returns a Web page may need to return (int,string) a tuple of type to describe the success or failure of a Web page. By returning two different values, each value type also has a different tuple, and the function can provide more output information, This is more effective than a single value that it can only return a single type.

Note the point:

Tuples are very effective when they are temporarily combined with interrelated values. But not suitable for creating complex data types. If you want to save a global data that is out of local scope, use a class or struct to be more appropriate.

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.