ios - Swift/iOS - 如何使用一个范围/函数中的值并将其传递给另一个?
问题描述
我试图将 gyroX 的值传递给另一个函数,但是当我在另一个函数中将它用作 gyroX 时,它最终的值为 0。
这是代码:
var gyroX = Float()
motion.startGyroUpdates(to: .main) { (data, error) in
if let myData = data {
gyroX = Float(myData.rotationRate.x)
}
}
解决方案
使用 Xcode 13 Beta 和 Swift 5.5
这是我们现在可以通过 Async/Await 的 Continuations 解决的问题
我们将首先创建一个函数,将回调转换为可等待的结果,例如:
func getXRotation(from motion: CMMotionManager) async throws -> Float {
try await withCheckedThrowingContinuation { continuation in
class GyroUpdateFailure: Error {} // make error to throw
motion.startGyroUpdates(to: .main) { (data, error) in
if let myData = data {
continuation.resume(returning: Float(myData.rotationRate.x))
} else {
throw GyroUpdateFailure()
}
}
}
}
然后我们可以分配变量并像这样使用它:
let gyroX = try await getXRotation(from: motion)
callSomeOtherFunction(with: gyroX)
使用 Xcode <= 12 并结合
在当前版本的 Swift 和 Xcode 中,我们可以使用 Combine 框架使回调处理对我们来说更容易一些。首先,我们将运动管理器的闭包转换为“Future”。然后我们可以在联合链中使用这个未来。
func getXRotation(from motion: CMMotionManager) -> Future<CMGyroData, Error> {
Future { promise in
class GyroUpdateFailure: Error {} // make error to throw
motion.startGyroUpdates(to: .main) { (data, error) in
if let myData = data {
promise(.success(myData))
} else {
promise(.failure(GyroUpdateFailure()))
}
}
}
}
// This is the other function you want to call
func someOtherFunction(_ x: Float) {}
// Then we can use it like so
_ = getXRotation(from: motion)
.eraseToAnyPublisher()
.map { Float($0.rotationRate.x) }
.map(someOtherFunction)
.sink { completion in
switch completion {
case .failure(let error):
print(error.localizedDescription)
default: break
}
} receiveValue: {
print($0)
}
合并流程有一些重要的部分。就是其中_ =
之一。在发布者上“下沉”的结果是一个“可取消”的对象。如果我们不将其存储在局部变量中,系统可以在任务执行之前清理任务。所以你肯定会想要这样做。
我强烈建议您查看 SwiftBySundell.com 以了解更多关于 Combine 或 Async/Await 和 RayWenderlich.com 的一般移动开发信息。
推荐阅读
- python - 使用 write_points 调用 Python influxdb-python,是否有任何可重试的错误?
- robotframework - 机器人框架元素可见性
- stata - Stata:更改图形条标签的字体
- c# - Make Socks 5 需要用户名/密码认证吗?钛网络代理
- sonarqube - 为什么声纳云不标记为“注入”?
- java - 无法解析 XML 文件
- node.js - 尝试学习 module.exports 但遇到了一些问题
- python - 内容 discord.py 的错误
- javascript - ReferenceError: err 未定义 mongoose mongodb
- flutter - Flutter/如何获取谷歌字体家族列表