Welcome to Swift (initial translation and annotations of Apple's official Swift document 14)---90~93 page (chapter II)

Source: Internet
Author: User

Debugging with assertions (assertion Debug)

An assertion is a run-time check mechanism that assumes that the logical condition is true. Before executing arbitrary code, you can use assertions to confirm a basic condition. If the condition is true, the code will continue to execute, and if the condition is false, the code will end and the application will exit.

If, in a debug environment, the running code triggers an assertion (such as when you compile and run an app in Xcode), you can clearly see where the state of the error is occurring and query the state of the application at that point in time. Assertions can also allow you to use the appropriate debug display information as the assertion information.

You can write a global function assert to trigger an assertion, pass to the function assert a true or false expression, and, if False, display a message:

Let Age =-3

ASSERT (age >= 0, "A person ' s cannot is less than zero")

This causes-assertion to trigger, because-is-not >= 0

In this code example, the code will continue to execute only when age >= 0 is established, that is, the value of age is non-negative. If the age value is negative, then the age>=0 condition is not valid, the assertion is triggered, and the program ends.

The assertion information cannot be inserted into a string, and if no information is required, it can be ignored like the following code:

ASSERT (age >= 0)

When to use assertions (what is the time to assert)

If the execution of the code must persist if a condition is true, then the condition can use the assertion in any potentially false case. The recommended assertion checks include:

      • An integer subscript passed to the index, which may be too small or too large.
      • The value passed to the function, the illegal value may not allow the function to complete the corresponding task.
      • An optional type value of nil for the current value, and subsequent code execution may give the optional type a non-nil value.

Note the point:

The assertion stops your app and ends, but the assertion is not intended to prevent unwanted illegal conditions in the code design. No matter what, there may always be illegal conditions in the code development phase, and assertions are an effective reminder of the importance of highlighting in this case (before you should publish).

Basic Operators (base operator)

Operators are specific symbols that can be used to check, change, or combine values. For example, the addition operator (+) adds two numbers together (for example, let i = 1+2). Complex also has logical and (and) operator && (e.g. if Entereddoorcode & & Passedretinascan) and the auto-increment operator ++i, which is an abbreviation for the I value to be increased by 1.

Swift supports most of the standard C operators and improves the ability to avoid errors in common code. The assignment operator (=) has no return value to prevent it from being mixed with the equality operator (= =). Mathematical operators (+,-, *,/,%, and so on) detect overflow of values in order to avoid unwanted results ( When the value size exceeds the storage range of the specified type).

Unlike the C language, the modulo operation (%) can be a floating-point value in Swift. The range operator is also available in Swift (for example, A.). B and a...b) represent a range, which is not found in the C language.

This chapter describes the general operators of Swift, which are described in Advanced Operators: How to customize the operation and use standard operators on their own definition types.

Terminology (terminology)

Monocular operator, binocular operator, trinocular operator:

      • The monocular operator operates only one target, (for example,-a), and the prefix unary operator appears before the target (for example,!b), and the suffix unary operator appears after the operation target (such as i++).
      • The binocular operator operates on two targets (for example, 2+3).
      • The trinocular operator operates three targets, just like the C language, there is only one three-mesh operator in Swift, and the Trinocular conditional operator (a? b:c)

The value of the operator operation, called the operand (or operand), in the expression 1+2, plus + is a binocular operator whose two operands are numeric 1 and the value 2.

/******************** to be Continued ... ************************/

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.