Swift: Assertion (assertions)

Source: Internet
Author: User

Assertion (assertions)

Optionals allows us to detect whether a value exists. In some cases, if a value does not exist or does not provide a specific satisfying condition, the code should not continue to execute down.

In these cases, you can use the trigger assertion to terminate execution and provide debugging.

The assertion is whether the condition is true at run time, and if true, continue down or it will be interrupted here.

assert that the corresponding keyword is assert

Such as:

  1. Let age = 18
  2. The result of the age >= 18 expression Here is true, so here's the assertion that nothing is done and the code continues to execute
  3. ASSERT (age >= 18, "a person ' a cannot is less than eighteen")
  4. Because the result of the age >= 22 expression is false, there is an interrupt here, which prints out
  5. "A person ' s age cannot is less than 22"
  6. ASSERT (Age > 22, "A person's age cannot is less than")


When do I use assertions?

Use assertions when the following conditions are included:

1. When an integer subscript index is passed as a value to a parameter implemented by a custom index, but the subscript index value cannot be too low or too high, use an assertion

2, pass the value to the function but if this pass-through value is invalid, the function cannot complete the function when the assertion is used.

3, the optional value is currently nil, but the following code successfully executes the condition is that the value cannot be nil, using assertions

Swift: Assertion (assertions)

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.