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:
Anycan represent an instance of any type at all, including function types.AnyObjectcan 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.
let optionalNumber: Int? = 3things.append(optionalNumber) // Warningthings.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 ...
随机推荐
- <video> controlsList
Audio/Video Updates in Chrome 58 <video controls controlsList="nofullscreen nodownload norem ...
- JavaScript DOM编程艺术(第2版)学习笔记2(4~6章应用实例)
本书的第4章使用第3章学到的操作DOM的方法和属性写了一个展示图片的网页,并在第5,6章对代码进行了优化. 第一版,搭建网页的静态结构,包括一级标题<h1>,无序列表清单<ul> ...
- LeetCode(11)Container With Most Water
题目如下: 题目的意思是求容器能装的最大的水量,当时我按梯形的面积来算,一直不对,后来才发现要按矩形的面积来算 Python代码如下: def maxArea(self, height): " ...
- 结构化编程-Structured programming
结构话编程强调的是对流程的控制: 它为面向过程编程提供天然的支持. Structured programming is a programming paradigm aimed at improvin ...
- CentOS 6.5下部署日志服务器 Rsyslog+LogAnalyzer+MySQL
简介 LogAnalyzer 是一款syslog日志和其他网络事件数据的Web前端.它提供了对日志的简单浏览.搜索.基本分析和一些图表报告的功能.数据可以从数据库或一般的syslog文本文件中获取,所 ...
- Eclipse中使用GIT将文件还原至上一版本
GIT将文件还原至上一版本: 选中文件——右击——Replace With——HEAD Revision:
- Codeforces Round #493 (Div. 2) A. Balloons 贪心水题
由于是输出任意一组解,可以将价值从小到大进行排序,第一个人只选第一个,第二个人选其余的.再比较一下第一个人选的元素和第二个人所选元素和是否相等即可.由于已将所有元素价值从小到大排过序,这样可以保证在有 ...
- jQuery优化性能的十种方法
1,总是从ID选择器开始继承 例如: <div id="content"> <form method="post" action=" ...
- 关于fromkeys的用法
分享一个小知识点: 1. Python 中关于dict的fromkeys方法: 1. fromkeys是用于重构字典 2. 至少传一个参数,第一个参数为新建dict的key,如果第一个参数为字典,那么 ...
- NOIP2018提高组省一冲奖班模测训练(四)
NOIP2018提高组省一冲奖班模测训练(四) 这次比赛只AC了第一题,而且花了40多分钟,貌似是A掉第一题里面最晚的 而且还有一个半小时我就放弃了…… 下次即使想不出也要坚持到最后 第二题没思路 第 ...