After reading about Swift Closures and trying to use the same syntax to pass a anonymous function into the handler I cannot get it to compile. How can I correctly re-create the following functioning objective c code in Swift?
在阅读了Swift Closures并尝试使用相同的语法将匿名函数传递给处理程序后,我无法进行编译。如何在Swift中正确地重新创建以下功能目标c代码?
Here is the objective c code that I am trying to re-create in swift
这是我试图在swift中重新创建的目标c代码
[self.motionManager
startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
withHandler:^(CMAccelerometerData *data, NSError *error)
{
dispatch_async(dispatch_get_main_queue(),
^{
float xx = data.acceleration.x;
float yy = -data.acceleration.y;
float angle = atan2(yy, xx);
self.dropitBehavior.gravity.angle = angle;
});
}
];
Here are few failed attempts at re-creating the code in swift:
以下是在swift中重新创建代码的失败尝试:
self.motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue(), withHandler: {
(data: CMAccelerometerData(), error: NSError()) -> Void = {
dispatch_async(dispatch_get_main_queue()) {
var xx = data.acceleration.x
var yy = -data.acceleration.y
var angle = atan2(yy, xx)
self.dropitBehavior.gravity.angle = angle
}
}
})
self.motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue(), withHandler: {
(data: CMAccelerometerData(), error: NSError()) {
dispatch_async(dispatch_get_main_queue()) {
var xx = data.acceleration.x
var yy = -data.acceleration.y
var angle = atan2(yy, xx)
self.dropitBehavior.gravity.angle = angle
}
}
})
1 个解决方案
#1
12
By doing CMAccelerometerData()
and NSError()
you are actually calling the initializers of those classes. You just need to use their types. However, because in objective-C, pointers can be nil, when you translate the types to Swift, you need to use optionals. The convention is to use Implicitly Unwrapped Optionals. Also, you separate the parameters of an anonymous closure with in
not additional curly brackets:
通过执行CMAccelerometerData()和NSError(),您实际上正在调用这些类的初始值设定项。你只需要使用他们的类型。但是,因为在objective-C中,指针可以是nil,当你将类型转换为Swift时,你需要使用选项。惯例是使用Implicitly Unwrapped Optionals。此外,您将匿名闭包的参数与其他大括号分开:
self.motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue(), withHandler: {
(data: CMAccelerometerData!, error: NSError!) in
// internal implementation
})
Also, because the types can be inferred from the parameter type, you don't have to even specify the types for the parameters:
此外,因为可以从参数类型推断出类型,所以您甚至不必指定参数的类型:
self.motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue(), withHandler: {
(data, error) in
// internal implementation
})
Also, if a block is the last parameter to a method / function call, you can define it outside of the parenthesis:
此外,如果块是方法/函数调用的最后一个参数,则可以在括号外定义它:
self.motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue()) {
(data, error) in
// internal implementation
}
This way you don't need the closing )
after the closure.
这样你就不需要在关闭后关闭了。
That creates the final version with your internal implementation:
这将创建内部实现的最终版本:
self.motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue()) {
(data, error) in
dispatch_async(dispatch_get_main_queue()) {
var xx = data.acceleration.x
var yy = -data.acceleration.y
var angle = atan2(yy, xx)
self.dropitBehavior.gravity.angle = angle
}
}
#1
12
By doing CMAccelerometerData()
and NSError()
you are actually calling the initializers of those classes. You just need to use their types. However, because in objective-C, pointers can be nil, when you translate the types to Swift, you need to use optionals. The convention is to use Implicitly Unwrapped Optionals. Also, you separate the parameters of an anonymous closure with in
not additional curly brackets:
通过执行CMAccelerometerData()和NSError(),您实际上正在调用这些类的初始值设定项。你只需要使用他们的类型。但是,因为在objective-C中,指针可以是nil,当你将类型转换为Swift时,你需要使用选项。惯例是使用Implicitly Unwrapped Optionals。此外,您将匿名闭包的参数与其他大括号分开:
self.motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue(), withHandler: {
(data: CMAccelerometerData!, error: NSError!) in
// internal implementation
})
Also, because the types can be inferred from the parameter type, you don't have to even specify the types for the parameters:
此外,因为可以从参数类型推断出类型,所以您甚至不必指定参数的类型:
self.motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue(), withHandler: {
(data, error) in
// internal implementation
})
Also, if a block is the last parameter to a method / function call, you can define it outside of the parenthesis:
此外,如果块是方法/函数调用的最后一个参数,则可以在括号外定义它:
self.motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue()) {
(data, error) in
// internal implementation
}
This way you don't need the closing )
after the closure.
这样你就不需要在关闭后关闭了。
That creates the final version with your internal implementation:
这将创建内部实现的最终版本:
self.motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue()) {
(data, error) in
dispatch_async(dispatch_get_main_queue()) {
var xx = data.acceleration.x
var yy = -data.acceleration.y
var angle = atan2(yy, xx)
self.dropitBehavior.gravity.angle = angle
}
}