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 ...
随机推荐
- Android PullToRrefresh 自定义下拉刷新动画 (listview、scrollview等)
PullToRefreshScrollView 自定义下拉刷新动画,只需改一处. 以下部分转载自http://blog.csdn.net/superjunjin/article/details/450 ...
- Android 自定义简单控件--星级评价
效果图 实现 package com.easypass.carstong.view; import android.content.Context; import android.content.re ...
- jtable的用法
一.创建表格控件的各种方式:1) 调用无参构造函数.JTable table = new JTable();2) 以表头和表数据创建表格.Object[][] cellData = {{" ...
- 页面定制CSS代码初探(六):h2、h3 标题自动生成序号 详细探索过程
前言 最近在整理博客写作格式的规范,碰到一个问题:标题要不要加序号? 直到我碰到一个人这么说 手动维护编号实在是一件很闹心的事情, 如果位置靠前的某个段落被删除了, 那么几乎每个段落的编号都要手动修改 ...
- Unity 向量点乘、叉乘
向量点乘计算角度,向量叉乘计算方位 a,b为向量 点乘计算公式:a x b = |a| x |b| x cosθ 叉乘计算公式:a x b = |a| x |b| x sinθ
- [THUWC2017]在美妙的数学王国中畅游 LCT+泰勒展开+求导
p.s. 复合函数求导时千万不能先带值,再求导. 一定要先将符合函数按照求导的规则展开,再带值. 设 $f(x)=g(h(x))$,则对 $f(x)$ 求导: $f'(x)=h'(x)g'(h(x)) ...
- Airtest ——poco
1. Pymysql(No module named ‘cryptography’) pip install cryptography pip install paramiko 把 cryptogr ...
- faker smtp server
import os import asyncio import logging import base64 from email import message_from_bytes from emai ...
- IOS - 总结(网络状态变更)
- (void)initNetworkMonitor { NSURL *baseURL = [NSURL URLWithString:@"http://www.baidu.com/" ...
- 安装laravel项目出错
我的本地是7.0.1,而下载下来的原来是运行在7.0.8版本的,于是执行:composer install 时,报了一堆错.解决办法:删掉composer.lock,重新composer instal ...