加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift3 – 设置Alamofire自定义目标文件名,而不是在Swift 3.0中

发布时间:2020-12-14 05:43:54 所属栏目:百科 来源:网络整理
导读:如何在swift 3.0中编写以下代码段?以下语法在swift 2中 Alamofire.download(.POST,invoice.url,parameters:params,destination: { (url,response) - NSURL in let pathComponent = response.suggestedFilename let fileManager = NSFileManager.defaultMana
如何在swift 3.0中编写以下代码段?以下语法在swift 2中
Alamofire.download(.POST,invoice.url,parameters:params,destination: { (url,response) -> NSURL in

        let pathComponent = response.suggestedFilename

        let fileManager = NSFileManager.defaultManager()
        let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory,inDomains: .UserDomainMask)[0]
        let fileUrl = directoryURL.URLByAppendingPathComponent(pathComponent)
        return fileUrl
    })
    .progress { bytesRead,totalBytesRead,totalBytesExpectedToRead in
        print(totalBytesRead)
        dispatch_async(dispatch_get_main_queue()) {
            let progress = Double(totalBytesRead) / Double(totalBytesExpectedToRead)
            completionHandler(progress,nil)
        }
    }
    .responseString { response in
        print(response.result.error)
        completionHandler(nil,response.result.error)
    }
在Swift 3中它就是这样的.
let parameters: Parameters = ["foo": "bar"]

let destination: DownloadRequest.DownloadFileDestination = { _,_ in
    let pathComponent = "yourfileName"
    let documentsURL = FileManager.default.urls(for: .documentDirectory,in: .userDomainMask)[0]
    let fileURL = documentsURL.appendPathComponent(pathComponent)
    return (fileURL,[.removePreviousFile,.createIntermediateDirectories])
}

Alamofire.download(urlString,method: .get,parameters: parameters,encoding: JSONEncoding.default,to: destination)
    .downloadProgress(queue: DispatchQueue.global(qos: .utility)) { progress in
        print("Progress: (progress.fractionCompleted)")
    }
    .validate { request,response,temporaryURL,destinationURL in
        // Custom evaluation closure now includes file URLs (allows you to parse out error messages if necessary)
        return .success
    }
    .responseJSON { response in
        debugPrint(response)
        print(response.temporaryURL)
        print(response.destinationURL)
    }

有关详细信息,请查看Alamofire DocumentationAlamofire 4.0 Migration Guide.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读