即使Swift被设置为Double,Swift也会给出无效的返回错误?
我收到此错误:即使Swift被设置为Double,Swift也会给出无效的返回错误?
/Users/xxxx/Desktop/xxx/xxx/ViewController.swift:43:38: Unexpected non-void return value in void function
和其他类似这样即使我的功能设置为双倍返还。
这是每个返回中出现此错误的函数之一。
func readWeight() -> Double {
let quantityType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMass)
let weightQuery = HKSampleQuery(sampleType: quantityType!, predicate: nil, limit: 1, sortDescriptors: nil) {
query, results, error in
if (error != nil) {
print(error!)
return 0.0
}
guard let results = results else {
print("No results of query")
return 0.0
}
if (results.count == 0) {
print("Zero samples")
return 0.0
}
guard let bodymass = results[0] as? HKQuantitySample else {
print("Type problem with weight")
return 0.0
}
return bodymass.quantity.doubleValue(for: HKUnit.pound())
}
healthKitStore.execute(weightQuery)
}
的是如何用一个实例:
print(readWeight())
谢谢!
必须在函数结束时返回Double值。
func readWeight() -> Double {
let quantityType = HKQuantityType.quantityType(forIdentifier:HKQuantityTypeIdentifier.bodyMass)
let weightQuery = HKSampleQuery(sampleType: quantityType!, predicate: nil, limit: 1, sortDescriptors: nil) {
query, results, error in
if (error != nil) {
print(error!)
return 0.0
}
guard let results = results else {
print("No results of query")
return 0.0
}
if (results.count == 0) {
print("Zero samples")
return 0.0
}
guard let bodymass = results[0] as? HKQuantitySample else {
print("Type problem with weight")
return 0.0
}
return bodymass.quantity.doubleValue(for: HKUnit.pound())
}
healthKitStore.execute(weightQuery)
return some double value here
}
我想你想达到什么是类似的东西:
func readWeight(result: @escaping (Double) -> Void) {
let quantityType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMass)
let weightQuery = HKSampleQuery(sampleType: quantityType!, predicate: nil, limit: 1, sortDescriptors: nil) {
query, results, error in
if (error != nil) {
print(error!)
result(0.0)
}
guard let results = results else {
print("No results of query")
result(0.0)
}
if (results.count == 0) {
print("Zero samples")
result(0.0)
}
guard let bodymass = results[0] as? HKQuantitySample else {
print("Type problem with weight")
result(0.0)
}
result(bodymass.quantity.doubleValue(for: HKUnit.pound()))
}
healthKitStore.execute(weightQuery)
}
然后你可以使用readWeight
方式类似:
readWeight(result: { myResult in
let comparisonResult = myResult == 0.0
})
所以这里myResult
是价值你正在寻找。
这一切都是由于HKSampleQuery
异步执行造成的,因此您无法立即知道返回值。你必须等待它的结果,然后把结果作为方法的参数给出一个闭包。在结束
这种工作,但我得到这个错误时,它比较它'/Users/xxx/Desktop/xx/xx/ViewController.swift:161:27:二进制运算符'=='不能应用于'if(self.readWeight == 0.0)' – WebMaster
@WebMaster类型'(@escaping(Double) - > Void) - >()'和'Double''的操作数请检查更新回答 – user3581248
的括号内的线路:
也许你应该多看一些关于关闭
let weightQuery = HKSampleQuery(sampleType: quantityType!, predicate: nil, limit: 1, sortDescriptors: nil) { ... }
实际发送另一个关闭工作的一部分作为HKSampleQuery
的初始值设定项的参数。把它们想象成一个独立的功能,当你拨打healthKitStore.execute(weightQuery)
时,它将被执行。如Apple's documentation中所述,该封闭的签名是:(HKSampleQuery, [HKSample]?, Error?) -> Void
。这意味着你不能从该闭包中的语句中返回任何东西。
牢记这一点,你可以修改你的方法如下:
func printWeight() {
let quantityType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMass)
let weightQuery = HKSampleQuery(sampleType: quantityType!, predicate: nil, limit: 1, sortDescriptors: nil) {
query, results, error in
if (error != nil) {
print(error!)
}
guard let results = results else {
print("No results of query")
}
if (results.count == 0) {
print("Zero samples")
}
guard let bodymass = results[0] as? HKQuantitySample else {
print("Type problem with weight")
}
print("Bodymass:", bodymass.quantity.doubleValue(for: HKUnit.pound()))
}
healthKitStore.execute(weightQuery)
}
,然后只需要调用printWeight()
得到的结果。
您需要使用块。所以函数本身将返回void。当HKSampleQuery
开始时,它会得到执行并等待结果,而您的readWeight
函数继续执行,然后结束返回void。此时,您的HKSampleQuery
仍在执行。完成后,它会通过完成处理程序发布结果。所以如果你想对结果Double
做任何事情,你需要在完成处理程序中完成。所以,你的功能将
func readWeight(completion: @escaping (Double) -> Void) {
let quantityType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMass)
let weightQuery = HKSampleQuery(sampleType: quantityType!, predicate: nil, limit: 1, sortDescriptors: nil) {
query, results, error in
if (error != nil) {
print(error!)
completion(0.0)
}
guard let results = results else {
print("No results of query")
completion(0.0)
}
if (results.count == 0) {
print("Zero samples")
completion(0.0)
}
guard let bodymass = results[0] as? HKQuantitySample else {
print("Type problem with weight")
completion(0.0)
}
completion(bodymass.quantity.doubleValue(for: HKUnit.pound()))
}
healthKitStore.execute(weightQuery)
}
要使用结果:
self.readWeight(){ (result) in
//This result is a double value that is returned from HKSampleQuery
}
你试图在你传递给'HKSampleQuery'块返回的东西,那是行不通的,块呢没有返回类型。你必须为你的readWeight方法使用完成块,或者以某种方式等待完成'HKSampleQuery'。 – luk2302
是'HKSampleQuery'函数是同步的吗?如果是这样,你需要使用块和完成处理程序 –
@FangmingNing刚学会它是异步的,所以我不得不等待值...这个https://stackoverflow.com/a/44701636/4285493似乎工作,但比较它似乎并没有工作 – WebMaster