Zhao yazhi _ Swift (4) _ asserted, Zhao yazhi _ swift asserted

Source: Internet
Author: User

Zhao yazhi _ Swift (4) _ asserted, Zhao yazhi _ swift asserted

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.

Use assertions for debugging

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") // The assertion is triggered because age <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 passed 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. However, invalid values may cause function execution to fail.
  • An optional value is now nil, but a non-nil value is required for subsequent code execution.

See the attached scripts and functions.

 

Note: assertions may cause your application to stop running, so you should carefully design your code so that invalid conditions do not appear. However, some illegal conditions may occur before your application is released. Using assertions can quickly discover problems.




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.