gpt4 book ai didi

ios - 快速关闭 : cannot invoke a function with its argument list

转载 作者:搜寻专家 更新时间:2023-11-01 05:40:10 26 4
gpt4 key购买 nike

我正在使用闭包编写一个 Swift 函数。一个应该可以编译的代码示例是这样的,

import Foundation

typealias PKSynchronizeProgressBlock = (Double) -> Void
typealias PKSynchronizeCompletionBlock = (Bool, NSError?) -> Void

class X {

func synchronizeAppDataWithProgress(
progress: PKSynchronizeProgressBlock?,
completion: PKSynchronizeCompletionBlock?) {
dispatch_async(dispatch_get_main_queue(), {

// Do a lot of downloading, and during the process
// {
// If progress is updated
if (progress != nil) {
progress!(Double(0))
}
//
// If something goes wrong
if (completion != nil) {
completion!(false, nil)
}
// }
dispatch_async(dispatch_get_main_queue(), {
if (completion != nil) {
completion!(true, nil)
}
})
})
}


func foo() {
self.synchronizeAppDataWithProgress({ (progress: Double) -> Void in
self.launchProgressBar.progress = progress
}, completion: { (success: Bool, error: NSError?) -> Void in
if success {
self.launchProgressBar.progress = 1.0
}
else {
print("Failed to synchronize app data with error %@", error!)
}
})
}

}

但是,这段代码无法编译。 Xcode 说

cannot invoke 'synchronizeAppDataWithProgress' with an argument list '(progress: (Double) -> Void, completion: (Bool, NSError?) -> Void)'

我该怎么办?我的代码中是否犯了任何愚蠢的错误?


更新:

感谢@Mario Zannone。我修复了上面代码中的前两个错误。那是: (1) 我在函数调用中插入了多余的 progress:。我已经删除了。 (2) 我在主线程以外的线程中更新了 UI。

但是如果我不注释掉 foo() 中的以下单行,代码仍然无法运行,

self.launchProgressBar.progress = progress

你知道为什么吗?

最佳答案

Xcode 有时会挑剔参数在闭包中列出的方式。我发现最好保留推断的类型。还要确保使用捕获列表来避免闭包中的强引用循环。

使用 Alamofire依赖项,我已经重写了上面的代码并且它可以编译。

import Alamofire

typealias ProgressBlock = (Double) -> Void
typealias CompletionBlock = (Bool, ErrorType?) -> Void

class ExampleDataSource {
func fetchData(progress: ProgressBlock?, completion: CompletionBlock?) {
// Here we use the Alamofire Dependency for progress reporting simplicity.
Alamofire.request(.GET, "https://www.myexampledomain.com")
.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
// bytesRead, totalBytesRead, and totalBytesExpectedToRead are Int64
// so we must perform unit conversion
let progressPercentage = Double(totalBytesRead) / Double(totalBytesExpectedToRead)
// here we optionally call the ProgressBlock 'progress' and pass it the progressPercentage
progress?(progressPercentage)
}
.response { request, response, data, error in
// here we usually parse the data, but for simplicity we'll
// simply check to see if it exists.
let completionFlag = (data != nil)
// note that NSError? is interchangable with ErrorType?
completion?(completionFlag, error)
}
}
func performTasks() {
// first we'll set up our closures...
let progressBlock: ProgressBlock = { progress in
// here we update the UI or whatever
// the nice thing about the Alamofire dependency is
// that it's thread-safe :]
}
let completionBlock: CompletionBlock = { success, error in
// here we do whatever we need to do when the
// network operation finishes, or handle the
// errors appropriately
}
// then we'll pass them into our fetchData method
fetchData(progressBlock, completion: completionBlock)
}
}

关于ios - 快速关闭 : cannot invoke a function with its argument list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31636821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com