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

swift语言点评十九-类型转化与检查的更多相关文章

  1. swift语言点评十八-异常与错误

    1.错误类型与枚举的结合 enum VendingMachineError: Error { case invalidSelection case insufficientFunds(coinsNee ...

  2. swift语言点评十六-Initialization && Deinitialization

    initial value:必须初始化.不影响观察者 Classes and structures must set all of their stored properties to an appr ...

  3. swift语言点评十四-继承

    Overriding A subclass can provide its own custom implementation of an instance method, type method, ...

  4. swift语言点评十-Value and Reference Types

    结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: f ...

  5. swift语言点评十五-$0

    import UIKitimport XCPlayground class ViewController: UIViewController { func action() { print(" ...

  6. swift语言点评十二-Subscripts

    Classes, structures, and enumerations can define subscripts, which are shortcuts for accessing the m ...

  7. Swift语言指南(十)--字符串与字符

    原文:Swift语言指南(十)--字符串与字符 字符串是一段字符的有序集合,如"hellow,world"或"信天翁".Swift 中的字符串由 String ...

  8. swift语言点评二十-扩展

    结论:扩展无法修改原来的数据结构. Extensions can add new functionality to a type, but they cannot override existing ...

  9. swift语言点评二

    一.数据类型 1.基础类型的封装 Swift provides its own versions of all fundamental C and Objective-C types, includi ...

随机推荐

  1. oracle插入或更新某一个指定列来执行触发器

    表结构: create table TZ_GXSX ( ID VARCHAR2(15), PROJECT VARCHAR2(50), TXYX NUMBER(22) default '0', CDAT ...

  2. 路飞学城Python-Day24

    12.粘包现象 客户端接收的信息指定了的字节,TCP协议没有丢失协议,只是只能接收指定的字节数,于是产生出了粘包现象 服务端接收命令只能接收1024字节,服务端执行命令结果以后传输给客户端,客户端再以 ...

  3. Pyhton学习——Day58

    From表单验证 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  4. easyUI datagrid的合并的js封装

    $.extend($.fn.datagrid.methods, { autoMergeCells : function (jq, fields) { return jq.each(function ( ...

  5. is == id 的用法;代码块;深浅copy;集合

    1 内容总览 is == id 用法 代码块 同一代码块下的缓存机制 (字符串驻留机制) 不同代码块下的缓存机制 (小数据池) 总结 集合(了解) 深浅copy 2 具体内容 id is == # i ...

  6. [BZOJ3438][洛谷P1361]小M的作物

    题目大意:有A.B两个集合和n个物品,每个物品只能放在一个集合里.每个物品放在不同集合内能获得不同价值.有一些物品,如果它们同时放在一个集合内,则会产生新的价值(A和B中都有且不一定相同(c1和c2) ...

  7. CVX安装使用

    CVX下载 下载地址 使用手册 Using Gurobi with CVX Using MOSEK with CVX CVX安装 下载压缩文件后解压缩至任意地址,打开matlab,进入解压缩后的地址, ...

  8. S5PV210 三个Camera Interface/CAMIF/FIMC的区别

    S5PV210有三个CAMIF单元,分别为CAMIF0 CAMIF1和CAMIF2.对应着驱动中的fimc0, fimc1, fimc2.在三星datasheet和驱动代码中CAMIF和FIMC(Fu ...

  9. pytorch 6 batch_train 批训练

    import torch import torch.utils.data as Data torch.manual_seed(1) # reproducible # BATCH_SIZE = 5 BA ...

  10. tomcat使用及原理

    1,Tomcat作为Servlet容器的基本功能 2,Tomcat的组成结构 Tomcat本身由一列的可配置的组件构成,其中核心组件是Servlet容器组件,它是所有其他Tomcat组件的顶层容器.T ...