Swift Language Guide (ii) basic operators

Source: Internet
Author: User

Operators are special symbols or phrases that are used to detect, change, or combine values. For example, the addition operator (+) adds two numbers together (such as let i = 1 + 2). More complex examples include logic and operators && (such as if Entereddoorcode && Passedretinascan) and the increment operator ++i, which is the shortcut to add the value of I to 1.

Swift supports most of the standard C operators and improves several features to eliminate common coding errors. The assignment operator (=) does not return a value in order to prevent the use of the equals operator (= =) accidentally as an assignment operator. The arithmetic operator (+,-, *,/,%, and so on) detects and disables the value overflow so that the result of the operation is greater than or less than the allowable range of the type.

Unlike C,swift, you can take a float (%) on a floating-point number. Swift also provides two range operators (a.). < B and A...b), which is not in C, is used to denote a range of values.

Now let's briefly introduce the commonly used operators in Swift.

Terms

The operators are divided into unary, two and ternary:

Unary operators operate on a single target (such as-a). Unary prefix operators appear immediately before their targets (such as!b), and unary suffix operators appear immediately after their targets (such as i++).
The binary operator operates on two targets (for example, 2 + 3), which is the infix operator because it appears in the middle of two targets.
The ternary operator operates on three targets. Like C, Swift has only one ternary operator, which is the ternary conditional operator (a? b:c).
The value that the operator affects is called the operand. In expression 1 + 2, the symbol + is a two-dollar operator whose operands are values 1 and 2.

One or one-tuple operator

(i), minus sign operator

(b), plus sign operator

Note: Multiply (*), except (/) same as OC usage

(iii), self-increment, self-decrement operator

Self-reduction Ibid, essentially no different from OC

Two or two-tuple operator

(i), assignment operators

  1. The assignment operator (a = b) Initializes or updates the value of a with the value of B:

2. If the right side of the assignment operator is a tuple with multiple values, its elements can be disassembled immediately to multiple constants or variables:

    3. Unlike the assignment operator in C and Objective-c, the assignment operator in Swift does not return a value on its own. The following statement is not possible:

This feature prevents the assignment operator (=) from being mistakenly used as the equals operator (= =). Swift helps you avoid if x = y these errors appear in the code.

(ii), the remainder operator

(c), combined assignment operator

(iv), comparison operators

Swift supports all standard C comparison operators, with the same functionality and usage as in C, and does not repeat

equals (A = = b)

Not equal to (a! = b)

Greater than (a > B)

Less than (a < b)

Greater than or equal to (a >= b)

Less than or equal to (a <= b)

 (v), logical operators

Swift supports three standard logic operators based on the C language:

Logical No (!A)

Logic with (a && b)

Logic or (a | | b)

The following two are the two new operators for Swift:

(vi), interval operator

1. Full closed interval

The Closed range operator (A...B) defines a range from a to B, including the values of A and B. The value of a must not be greater than B.

2, half open half-closed interval

Semi-open range is particularly useful for lists starting from 0, such as arrays, always counting to (but not including) the length of the list

 (vii), empty-fit operators

The empty-fit operator ( a ?? b ) makes an empty judgment on the optional type a and a returns a default value if it contains a value to be unpacked b . This operator has two conditions:

  The expression a must be of type optional

  bthe type of the default value must be a consistent with the type of the stored value

three or three-tuple operator

The ternary conditional operator is divided into three parts, in the form of such question? Answer1:answer2.

For example: a > B? A:b

Use the three-wood operator with the C language, and don't repeat it here.

Swift Language Guide (ii) basic operators

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.