Swift 3:Transfer Utility enumerateToAssignBlocks方法签名

时间:2017-01-24 10:51:43

标签: ios swift amazon-web-services amazon-s3 swift3

官方AWS文档包含以下Objective-c代码

  @IBOutlet weak var heightConstraint: NSLayoutConstraint!// tableView height constraint 
     func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
                self.heightConstraint.constant = tableView.contentSize.height
            }

我尝试将其转换为Swift 3(仅限上传任务)

 AWSS3TransferUtility *transferUtility = [AWSS3TransferUtility defaultS3TransferUtility];
[transferUtility
 enumerateToAssignBlocksForUploadTask:^(AWSS3TransferUtilityUploadTask *uploadTask, __autoreleasing AWSS3TransferUtilityUploadProgressBlock *uploadProgressBlockReference, __autoreleasing AWSS3TransferUtilityUploadCompletionHandlerBlock *completionHandlerReference) {
     NSLog(@"%lu", (unsigned long)uploadTask.taskIdentifier);

     // Use `uploadTask.taskIdentifier` to determine what blocks to assign.

     *uploadProgressBlockReference = // Reassign your progress feedback block.
     *completionHandlerReference = // Reassign your completion handler.
 }
 downloadTask:^(AWSS3TransferUtilityDownloadTask *downloadTask, __autoreleasing AWSS3TransferUtilityDownloadProgressBlock *downloadProgressBlockReference, __autoreleasing AWSS3TransferUtilityDownloadCompletionHandlerBlock *completionHandlerReference) {
     NSLog(@"%lu", (unsigned long)downloadTask.taskIdentifier);

     // Use `downloadTask.taskIdentifier` to determine what blocks to assign.

     *downloadProgressBlockReference =  // Reassign your progress feedback block.
     *completionHandlerReference = // Reassign your completion handler.
 }];

但Xcode会抛出以下错误

  

无法将type() - >()的值转换为预期的参数类型[...]

任何人都可以在Swift 3中工作吗?可能方法签名已经改变

修改

我申请了@donkon回答:

transferUtility.enumerateToAssignBlocks(forUploadTask:
        {

            let progressPointer = AutoreleasingUnsafeMutablePointer<AWSS3TransferUtilityUploadProgressBlock?>(&uploadProgressBlock)
            let completionPointer = AutoreleasingUnsafeMutablePointer<AWSS3TransferUtilityUploadCompletionHandlerBlock?>(&completionBlock)

            progress = progressPointer
            completion = completionPointer

    }
        , downloadTask: nil)

现在Xcode告诉进度和完成都是值,无法更改。

2 个答案:

答案 0 :(得分:3)

您尝试使用的方法包含函数参数。 SDK中的这些函数是用Objective-C编写的。因此,您应该使用@convention(block)来表明它们是与Objective-C兼容的块引用。

AWSS3TransferUtility.default().enumerateToAssignBlocks(forUploadTask: { (uploadTask:AWSS3TransferUtilityUploadTask, uploadProgressBlockReference:AutoreleasingUnsafeMutablePointer<(@convention(block) (AWSS3TransferUtilityTask, Progress) -> Void)?>?, completionHandlerReference: AutoreleasingUnsafeMutablePointer<(@convention(block) (AWSS3TransferUtilityUploadTask, Error?) -> Void)?>?) in
    print("Hello, world")

    uploadProgressBlockReference?.pointee = {(task:AWSS3TransferUtilityTask, progress:Progress) -> Void in
        print("Fraction completed:  \(progress.fractionCompleted)")
    }
}, downloadTask: nil)

答案 1 :(得分:0)

不值得立即尝试那种方法,而绕开它。我很确定这可以完成相同的操作。

 func handleForeground() {
    if let multiPartUploadTasks = self.transferUtility.getMultiPartUploadTasks().result, let uploads = multiPartUploadTasks as? [AWSS3TransferUtilityMultiPartUploadTask] {
        for upload in uploads {
            upload.setProgressBlock(self.progressBlock)
            upload.setCompletionHandler(self.completionBlock)
        }
    }
}