ios 无法调用非函数类型的值 '((success: Bool, error: NSError?) throws -> Void)?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32764215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot call value of non-function type '((success: Bool, error: NSError?) throws -> Void)?)
提问by mosaic6
I updated to Swift 2 and Xcode 7 and ran the migration tool. Then I got a ton of errors. One I'm stuck on is this
我更新到 Swift 2 和 Xcode 7 并运行迁移工具。然后我得到了很多错误。我坚持的一个是这个
func authorizeHealthKit(completion: ((success:Bool, error:NSError?) throws -> Void)?)
{
// 1. Set the types you want to read from HK Store
let healthKitTypesToRead = (array:[
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)
])
// 2. Set the types you want to write to HK Store
let healthKitTypesToWrite = (array:[
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)
])
// 3. If the store is not available (for instance, iPad) return an error and don't go on.
if !HKHealthStore.isHealthDataAvailable()
{
var error = NSError.self
if( completion != nil )
{
completion(success:false, error:&error)
}
return;
}
// 4. Request HealthKit authorization
healthKitStore.requestAuthorizationToShareTypes(healthKitTypesToWrite as Set<NSObject>, readTypes: healthKitTypesToRead as Set<NSObject>) { (success, error) -> Void in
if( completion != nil )
{
completion(success:success,error:error)
}
}
}
The error is on completion(success:false, error&error)
错误正在完成(成功:错误,错误和错误)
Any thoughts?
有什么想法吗?
回答by Rob
You are calling the closure like so:
你像这样调用闭包:
if completion != nil {
completion(success: false, error: error)
}
The completion
is optional, so you would call it as:
该completion
是可选的,所以你会称呼其为:
completion?(success: false, error: error)
Note the ?
. It also eliminates the need for the if completion != nil ...
.
请注意?
. 它还消除了对if completion != nil ...
.
--
——
I notice that you've defined the closure such that it throws errors. If that's really the case, then you need something like a do
-try
-catch
block:
我注意到您已经定义了闭包,因此它会引发错误。如果确实如此,那么您需要像do
- try
-catch
块这样的东西:
do {
try completion?(success:false, error: error)
} catch let completionError {
print(completionError)
}
Either that, or change the closure so that it doesn't throw errors. That's a pretty curious pattern.
要么,要么更改闭包,以免引发错误。这是一个非常奇怪的模式。
--
——
You also have a line that says
你也有一行说
var error = NSError.self
I don't know what your intent of that is. What error are you trying to pass back to the closure?
我不知道你这样做的意图是什么。您试图将什么错误传递回闭包?
If you really wanted to create your own NSError
object, you might do something like:
如果您真的想创建自己的NSError
对象,您可以执行以下操作:
let error = NSError(domain: "com.domain.app", code: 1, userInfo: [NSLocalizedDescriptionKey : NSLocalizedString("Health Data Not Available", comment: "")])
回答by Honey
I was getting a very similar error message for a completely different reason.
由于完全不同的原因,我收到了非常相似的错误消息。
Cannot call value of non-function type CLLocationCoordinate2D?
无法调用非函数类型 CLLocationCoordinate2D 的值?
I wrote my code as such:
我这样写我的代码:
secondCoord(latitude: secondLongLat.0, longitude: secondLongLat.1)
Which was foolish. I was missing the constructor itself. The correct version is below.
这是愚蠢的。我错过了构造函数本身。正确的版本如下。
secondCoord = CLLocationCoordinate2D(latitude: secondLongLat.0, longitude: secondLongLat.1)