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
相关文章
- go语言中获取变量类型的三种方法
- 17、屏幕适配,多语言支持,手机类型适配
- Linux - 终端语言设置
- Go语言的类型转换和类型断言
- R语言因子
- R语言作为BI中ETL的工具
- 单语言精简版win10下中文网页无法正常
- 17、屏幕适配,多语言支持,手机类型适配
- Atitit 编程语言语言规范总结 目录 1. 语言规范3 2. Types3 2.1.1. Primitive types3 2.1.2. Compound types4 3. State
- AI:大力出奇迹?Bigger is better?AI下一代浪潮?预训练大语言模型的简介(起源/目的/概述/技术基础/核心/影响/优缺点/未来趋势)、发展史、模型分类及其对比、案例应用之详细攻略
- 我的Go+语言初体验——【四、版本更新环境变量配置】
- 买票需要的时间-c语言
- 查询无效交易-c语言
- 链式栈及其基本操作c语言实现
- 【一天一门编程语言】Lisp 语言程序设计极简教程
- 【Groovy】Groovy 动态语言特性 ( Groovy 中的变量自动类型推断以及动态调用 | Java 中必须为变量指定其类型 )
- 使用内联中间语言
- golang go语言通道类型的通道示例 通道的通道
- 回顾最初学习语言的编译环境:在Windows10终端编译运行C/C++程序
- go语言笔记——数组长度不可变,但是元素值是可变的!!!
- IPython:利用python语言将后缀为ipynb文件中的输出的图片在py文件中编程进行可视化—即如何将IPython.core.display.HTML类型的数据进行图表可视化