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. html5中canvas(2)

    1.绘制图片(drawImage)(重点) 1.1 基本绘制图片的方式 context.drawImage(img, x, y); 参数: img 可以为:图片.视频或者canvas画布 x,y 绘制 ...

  2. Primitive data types recap

  3. 2018年江西理工大学C语言程序设计竞赛高级组部分题解

    B Interesting paths 考察范围:组合数学 此题是机器人走方格的变种,n*m的网格,从(1,1)走到(n,m),首先可以明确,水平要走m-1格,竖直要走n-1格,则走到目的地的任意一条 ...

  4. MyEclipse如何设置自动提示?

    MyEclipse --> Preferences --> Java --> Editor --> Content Assist --> Enable auto acti ...

  5. Kattis - Association for Computing Machinery

    Association for Computing Machinery ACM (Association for Computing Machinery) organizes the Internat ...

  6. zabbix监控自身为监控机(server)

    Zabbix 监控主机 添加自身为被监控机 这里我的环境已经部署完毕(没有经验的小伙伴可以看我上一篇文章) 等待一会刷新如下页面 字体出现以下亮度就成功了

  7. 洛谷T44252 线索_分治线段树_思维题

    分治线段树,其实就是将标记永久化,到最后再统一下传所有标记. 至于先后顺序,可以给每个节点开一个时间戳. 一般地,分治线段树用于离线,只查询一次答案的题目. 本题中,标记要被下传 222 次. Cod ...

  8. redis 篇 - set

    set 无序集合 sadd key value 127.0.0.1:6379[7]> sadd s 3 (integer) 1 127.0.0.1:6379[7]> smembers s ...

  9. BZOJ 3282 Link Cut Tree (LCT)

    题目大意:维护一个森林,支持边的断,连,修改某个点的权值,求树链所有点点权的异或和 洛谷P3690传送门 搞了一个下午终于明白了LCT的原理 #include <cstdio> #incl ...

  10. 紫书 例题7-14 UVa 1602(搜索+STL+打表)

    这道题想了很久不知道怎么设置状态,怎么拓展,怎么判重, 最后看了这哥们的博客 终于明白了. https://blog.csdn.net/u014800748/article/details/47400 ...