Swift learning-Swift BASICS (8)

Source: Internet
Author: User

Assertions assertions

Optional. You can determine whether a value exists. You can optimize the processing of missing values in the code. However, in some cases, if the value is missing or does not meet specific conditions, your code may not need to be executed. In this case, you can trigger an assertion in your code to end code running and debug the code to find the cause of missing values.

 


Debugging with assertions debug with assertions

The assertion checks whether a logical condition is true at runtime. Literally, the asserted condition is true. You can use assertions to ensure that some important conditions have been met before other code is run. If the condition is true, the Code continues to run. If the condition is false, the Code stops running and your application is terminated.

If your code triggers an asserted in the debugging environment, for example, you build and run an application in xcode, you can clearly see where the illegal status occurs and check the status of your application when the assertions are triggered. Additionally, Assertions allow you to append a piece of debugging information.

You can use the global assert function to write an asserted. Input a true or false expression and a message to the assert function. When the expression is false, this message is displayed:

Let age =-3 assert (age> = 0, "a person's age cannot be less than zero") // This causes the assertion to trigger, because age is not> = 0

In this example, the code runs only when age> = 0 is true, that is, when the value of age is not negative. If the value of age is negative, as in the code, age> = 0 is false, the assertion is triggered, and the application is terminated.

You cannot use string interpolation for asserted information. The asserted information can be omitted, as shown in the following figure:

 
Assert (age> = 0)


When to use assertions

Use assertions when the condition may be false, but make sure that the condition is true in the end so that your code can continue to run. Applicable scenarios of assertions:

-The index of the accessory script of the integer is imported into a custom accessory script, but the index value of the subscript may be too small or too large.

-You need to input a value to the function, but the invalid value may cause the function to fail to run normally.

-An optional value is now nil, but a non-nil value is required for subsequent code execution.


Okay, the above is all about Swift's first chapter. The basic part is the basics.

The links to all translated articles in Chapter 1 are as follows:

Swift learning-Swift BASICS (1)

Swift learning-Swift BASICS (2)

Swift learning-Swift BASICS (3)

Swift learning-Swift BASICS (IV)

Swift learning-Swift BASICS (5)

Swift learning-Swift BASICS (6)

Swift learning-Swift BASICS (7)

Swift learning-Swift BASICS (8)


Swift learning-Swift BASICS (8)

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.