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 Any
value, you can use the as
operator to explicitly cast the optional to Any
, as shown below.
let optionalNumber: Int? = 3
things.append(optionalNumber) // Warning
things.append(optionalNumber as Any) // No warning
swift语言点评十九-类型转化与检查的更多相关文章
- swift语言点评十八-异常与错误
1.错误类型与枚举的结合 enum VendingMachineError: Error { case invalidSelection case insufficientFunds(coinsNee ...
- swift语言点评十六-Initialization && Deinitialization
initial value:必须初始化.不影响观察者 Classes and structures must set all of their stored properties to an appr ...
- swift语言点评十四-继承
Overriding A subclass can provide its own custom implementation of an instance method, type method, ...
- swift语言点评十-Value and Reference Types
结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: f ...
- swift语言点评十五-$0
import UIKitimport XCPlayground class ViewController: UIViewController { func action() { print(" ...
- swift语言点评十二-Subscripts
Classes, structures, and enumerations can define subscripts, which are shortcuts for accessing the m ...
- Swift语言指南(十)--字符串与字符
原文:Swift语言指南(十)--字符串与字符 字符串是一段字符的有序集合,如"hellow,world"或"信天翁".Swift 中的字符串由 String ...
- swift语言点评二十-扩展
结论:扩展无法修改原来的数据结构. Extensions can add new functionality to a type, but they cannot override existing ...
- swift语言点评二
一.数据类型 1.基础类型的封装 Swift provides its own versions of all fundamental C and Objective-C types, includi ...
随机推荐
- [转]opencv学习资料
转自:http://blog.csdn.net/poem_qianmo/article/details/20537737 1:Mat imread(const string& filename ...
- golang下使用ini配置文件(widuu/goini)
在“widuu/goini”基础上进行了修改,增加了其他数据类型配置值(string.int.int32.int64.[]int.[]string)的支持. 使用方法: ConfigCentor := ...
- protocol 和delegate(协议和代理)的区别
定义 protocol:中文叫协议,一个只有方法体(没有具体实现)的类,Java中称作接口,实现协议的类必须实现协议中@required标记的方法(如果有的话): delegate:中文叫代理或委托, ...
- flex-2
1. 2. justify:整理版面 3. 4.归纳 justify-content:flex-start(默认).center.flex-end 下面还会提到剩下的两种项目在主轴上对齐方式: spa ...
- Laravel源码解析之反射的使用
前言 PHP的反射类与实例化对象作用相反,实例化是调用封装类中的方法.成员,而反射类则是拆封类中的所有方法.成员变量,并包括私有方法等.就如"解刨"一样,我们可以调用任何关键字修饰 ...
- Vue2+Webpack+ES6 兼容低版本浏览器(IE9)解决方案
Vue2+Webpack+ES6 兼容低版本浏览器(IE9)解决方案 解决方式:安装 "babel-polyfill" 即可. 命令:npm install --save-dev ...
- 2015 Multi-University Training Contest 5 hdu 5349 MZL's simple problem
MZL's simple problem Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- POJ 2888
思路挺清晰的.不过,我就是WA.不清楚为什么,很多数据都过了. 其实,一个置换后若有循环节个数为K,则N必定可以除以尽K.而K正好可以看成一个环.为什么呢?看前K个珠子,就是一个环,而后面的若干个K个 ...
- MySQL高可用系列之MHA(二)
一.參数说明 MHA提供了一系列配置參数.深入理解每一个參数的详细含义,对优化配置.合理使用MHA非常重要.非常多高可用性也都是通过合理配置一些參数而实现的. MHA包含例如以下配置參数,分别说明例如 ...
- openssl之EVP系列之10---EVP_Sign系列函数介绍
openssl之EVP系列之10---EVP_Sign系列函数介绍 ---依据openssl doc/crypto/EVP_SignInit.pod翻译 (作者:DragonKing, ...