Swift in Try

Source: Internet
Author: User

There is such a keyword in swift: try? and try! , what are their application scenarios?

try?:

1. Application scenario: When a function or method throws an exception, the exception can be converted to an optional type, return a nil, the program does not crash, if the program is normal, it will return a correct optional type;

Eg: letanyobject = try? Nsjsonserialization.jsonobjectwithdata (Jsondata, Options:. Mutablecontainers)

2. Format: let+ attribute + try? + Functions or methods;

3. Note: The returned is an optional type, note that when mixed with other methods, there are multiple package options for the type of unpacking problem.

try!:

1. Meaning: Wrong wrong transmission chain, try! broke the wrong propagation chain, but if there is a real error in the run-time error, resulting in a program crash.

2. Format: let+ properties + try! + Functions or methods;

3. Note: If an error occurs, a run-time error occurs, causing the program to crash.

Swift in Try

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.