Swift as 向下转型的几种形式和用法

as?
for casts that are allowed to fail. This would happen if the object is nil or doesn’t have a type that is compatible with the one you’re trying to cast to. It will try to cast to the new type and if it fails, then no biggie. This cast returns an optional that you can unwrap with if let.
as!
for casts between a class and one of its subclasses. This is also known as a downcast. As with implicitly unwrapped optionals this cast is potentially unsafe and you should only use as! when you are certain it cannot possibly go wrong. You often need to use this cast when dealing with objects coming from UIKit and the other iOS frameworks. Better get used to all those exclamation marks!
as
for casts that can never possibly fail. Swift can sometimes guarantee that a type cast will always work, for example between NSString and String. In that case you can leave off the ? or the ! and just write as.

It can sometimes be confusing to decide which of these three cast operators you need. If so, just type “as” and Xcode will suggest the correct variant. But nine times out of ten it will be as!.有时候,这三种用法会容易混淆,Swift推荐使用as,但有时候用as!更合适

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Swift 中,主要有以下三种计时器: 1. `Timer`:这是一个基础的计时器,可以周期性地执行某些任务。 2. `DispatchSourceTimer`:这是一个基于 GCD 的计时器,可以更加精确地执行任务。 3. `CADisplayLink`:这是一个适用于游戏和动画的计时器,可以根据屏幕的刷新率来调整任务的执行时间。 下面分别介绍它们的使用方法: ### Timer 使用 `Timer` 类可以创建一个基础的计时器。以下是一个示例: ```swift class ViewController: UIViewController { var timer: Timer? var counter = 0 override func viewDidLoad() { super.viewDidLoad() // 创建计时器,并设置时间间隔为1秒 timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true) } @objc func updateTimer() { counter += 1 print("计时器已运行\(counter)秒") } // 在需要停止计时器的地方调用 invalidate() 方法 // timer?.invalidate() } ``` 在上面的示例中,我们创建了一个计时器,并设置时间间隔为1秒。每当计时器时间间隔到达时,都会调用 `updateTimer` 方法,其中我们可以执行一些操作,比如更新 UI 或者执行一些逻辑。在需要停止计时器的地方,可以调用 `invalidate()` 方法来停止计时器。 ### DispatchSourceTimer 使用 `DispatchSourceTimer` 类可以创建一个更加精确的计时器。以下是一个示例: ```swift class ViewController: UIViewController { var timer: DispatchSourceTimer? var counter = 0 override func viewDidLoad() { super.viewDidLoad() // 创建计时器,设置时间间隔为1秒 timer = DispatchSource.makeTimerSource(queue: DispatchQueue.main) timer?.schedule(deadline: .now(), repeating: .seconds(1)) timer?.setEventHandler(handler: { [weak self] in self?.counter += 1 print("计时器已运行\(self?.counter ?? 0)秒") }) // 启动计时器 timer?.resume() } // 在需要停止计时器的地方调用 cancel() 方法 // timer?.cancel() } ``` 在上面的示例中,我们创建了一个计时器,并设置时间间隔为1秒。每当计时器时间间隔到达时,都会调用 `setEventHandler` 方法中的闭包,其中我们可以执行一些操作,比如更新 UI 或者执行一些逻辑。在需要停止计时器的地方,可以调用 `cancel()` 方法来停止计时器。 ### CADisplayLink 使用 `CADisplayLink` 类可以创建一个适用于游戏和动画的计时器。以下是一个示例: ```swift class ViewController: UIViewController { var displayLink: CADisplayLink? var startTime: CFTimeInterval = 0 override func viewDidLoad() { super.viewDidLoad() // 创建计时器 displayLink = CADisplayLink(target: self, selector: #selector(updateTimer)) displayLink?.add(to: .main, forMode: .common) // 记录开始时间 startTime = CACurrentMediaTime() } @objc func updateTimer() { let currentTime = CACurrentMediaTime() let elapsedTime = currentTime - startTime print("计时器已运行\(elapsedTime)秒") } // 在需要停止计时器的地方调用 invalidate() 方法 // displayLink?.invalidate() } ``` 在上面的示例中,我们创建了一个计时器,并使用 `add(to:forMode:)` 方法将其添加到主循环中。每当屏幕需要刷新时,都会调用 `updateTimer` 方法,其中我们可以执行一些操作,比如更新游戏或者动画。在需要停止计时器的地方,可以调用 `invalidate()` 方法来停止计时器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值