zoukankan      html  css  js  c++  java
  • swift语言点评十九-类型转化与检查

    1、oc比较:

    -(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例

    -(BOOL) isMemberOfClass: classObj 判断是否是这个类的实例

    2、is 类型检查

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

    3、 (as? or as!) 类型转化

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

    Use the forced form of the type cast operator (as!) only when you are sure that the downcast will always succeed. 

    确信与不确信转化。

    4、Type Casting for Any and AnyObject

    Swift provides two special types for working with nonspecific types:

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

    • AnyObject can represent an instance of any class type.

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

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

    1. let optionalNumber: Int? = 3
    2. things.append(optionalNumber) // Warning
    3. things.append(optionalNumber as Any) // No warning
  • 相关阅读:
    vim字符串替换命令
    Android中View的事件分发机制——Android开发艺术探索笔记
    jQuery源代码框架思路
    C指针——C语言手记
    Python基础二--基本控制语句
    C++中的链式操作
    求一个字串中最长的连续字符串
    C# -- 推断字符能否转化为整形
    Loadrunner检查点使用总结
    LoadRunner设置检查点的几种方法介绍
  • 原文地址:https://www.cnblogs.com/feng9exe/p/8744098.html
Copyright © 2011-2022 走看看