Swift Language Reviews 19-type conversions and checks

Source: Internet
Author: User
Tags type casting

1, OC Comparison:

-(BOOL) Iskindofclass:classobj to determine if this class or subclass of this class is an instance of

-(BOOL) ismemberofclass:classobj to determine if it is an instance of this class

2. is type check

Use the type check operator ( is ) to check for whether an instance are of a certain subclass type.

3, ( as? or as! ) type conversion

Use the conditional form of the the type cast operator () when you were not as? sure if the downcast would succeed.

Use the forced form of the the type cast operator () only if you were sure that the downcast would always as! succeed.

Certainty and uncertainty of conversion.

4. Type Casting for any and anyobject

Swift provides-special types for working with nonspecific types:

    • AnyCan represent an instance of any type at all, including function types.

    • AnyObjectCan represent an instance of any class type.

To discover the specific type of a constant or variable that's known only to being of type Any or AnyObject , can use a or as pattern in a switch statement ' s cases. The

Anythe type represents values of any type, including optional types. Swift gives you a warning if your use an optional value where a value of type is Any expected. If you really does need to use an optional value Any as a value, you can use the as operator to explicitly cast the OPT Ional Any to, as shown below.

    1. let optionalNumber: Int? = 3
    2. things.append(optionalNumber) // Warning
    3. things.append(optionalNumber as Any) // No warning

Swift Language Reviews 19-type conversions and checks

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.