简体   繁体   English

iOS-无法调用非功能类型“ NSProgress”的值

[英]iOS - Cannot call value of non-function type 'NSProgress'

I'm trying to download a file using Alamofire, with progress. 我正在尝试使用Alamofire下载文件,并取得进展。 But I get this error at the progress line. 但是我在progress行上收到此错误。

Cannot call value of non-function type 'NSProgress'

What's the problem? 有什么问题? All the examples I've seen, namely the official one , do the exact same thing! 我所看到的所有示例(即官方示例)都做同样的事情!

Alamofire.download(.GET, nsurl!.absoluteString,     destination: { (temporaryURL, response) in
    return filepath
})
    .progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) in // <------ ERROR HERE
        dispatch_async(dispatch_get_main_queue(), {
            self.progressBar?.progress = totalBytesRead / totalBytesExpectedToRead
        })
    }
    .response { (request, response, _, error) in
        self.loadImageFromFile(filepath.absoluteString)
}

在此处输入图片说明

EDIT: 编辑:

After @mvoelkl's suggestion, I added parenthesis to the progress function. 在@mvoelkl的建议之后,我在进度函数中添加了括号。 But the result stayed the same: 但是结果保持不变: 在此处输入图片说明

This worked for me. 这对我有用。

Alamofire.download(.GET, nsurl!.absoluteString, destination: { (temporaryURL, response) in
    return file path
})
    .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
        let status = Float(totalBytesRead/totalBytesExpectedToRead)
        dispatch_async(dispatch_get_main_queue(), {
            self.progressBar?.progress = status
        })
    }
    .response { (request, response, _, error) in
        self.loadImageFromFile(filepath.absoluteString)
}

What may not work in the above is totalBytesExpectedToRead . 什么可能无法在上述工作是totalBytesExpectedToRead。 It sometimes returns -1, so you may need plug the size of the file you are downloading without resorting to Alamofire. 有时返回-1,因此您可能需要插入要下载文件的大小,而无需使用Alamofire。

For me the solution was to add Float around progress.fractionCompleted, so use it like this: 对我来说,解决方案是在progress.fractionCompleted周围添加Float,所以请像这样使用它:

.uploadProgress{ progress in
    self.updateProgressBar(imagesToDo: images.count, prog: Float(progress.fractionCompleted))
}

thanks to: https://github.com/Alamofire/Alamofire/issues/1652#issuecomment-259020449 感谢: https : //github.com/Alamofire/Alamofire/issues/1652#issuecomment-259020449

I hope this issue fix when update your Alamofire pod . 我希望在更新Alamofire pod时解决此问题。

Here mention this issue :- https://github.com/SwiftyJSON/Alamofire-SwiftyJSON/issues/40 在这里提到这个问题: -https : //github.com/SwiftyJSON/Alamofire-SwiftyJSON/issues/40

Had the same issue, fixed by using the following code: 使用以下代码修复了相同的问题:

Alamofire.upload(.PUT, url, file: file)
    .progress({ (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
        print("test")
    })

Mind the added parentheses for .progress(), found it by generating the code stub with Xcode. 注意为.progress()添加的括号,通过使用Xcode生成代码存根找到它。 Seems there's a mistake in the documentation, if you can confirm this we should file an issue on GitHub ;) 似乎文档中有错误,如果您可以确认这一点,我们应该在GitHub上提交问题;)

This issue plagued me too. 这个问题也困扰着我。 It was caused by an ambiguous issue with NSURLRequest.progress and Alamofire.Request.progress. 这是由NSURLRequest.progress和Alamofire.Request.progress的模棱两可的问题引起的。

To fix the issue I modified Alamofire's source. 为了解决此问题,我修改了Alamofire的源代码。

Open Alamofire/Request.swift (v3.4.x) 打开Alamofire / Request.swift(v3.4.x)

On line 141, rename the function to something that is not progress, I chose requestProgress. 在第141行,将函数重命名为未进行处理的东西,我选择了requestProgress。

From: 从:

public func progress(closure: ((Int64, Int64, Int64) -> Void)? = nil) -> Self {

To: 至:

public func requestProgress(closure: ((Int64, Int64, Int64) -> Void)? = nil) -> Self {

Proceed to use the function as before with no compiler issues. 继续像以前一样使用该函数,没有编译器问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM