Swift 类型转换

Swift 类型转换 as 的使用
if let as
类型转换,此时 btn
是 Any
类型,使用 as?
将他尝试转为 UIButton
类型。并赋值给 a
。 实际开发中建议 a
与 btn
命名一致,这里为了便于区分
1 |
|
guard let as
和 if let as
基本一致,只是当转换失败时提前退出。适用于一些异步回调里。
1 | guard let btn = btn as? UIButton else { return } |
- Post title:Swift 类型转换
- Post author:xxxixxxx
- Create time:2020-12-21 16:57:00
- Post link:https://xxxixxx.github.io/2020/12/21/100-Swift-类型转换/
- Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.
Comments