[弱自我]做什么以及这种代码结构意味着什么?

时间:2016-12-17 09:17:18

标签: swift2

我对以下代码的结构有几个问题。我假设progressBlock和completionhandlers是传递给downloadWithDownloadType函数的回调函数。我的假设是否正确?在函数参数之前做什么[弱自我]呢?在什么情况下你需要那个?

func downloadContent(key: String, pinOnCompletion: Bool) {

            let manager = AWSUserFileManager.defaultUserFileManager()
            let content = manager.contentWithKey(self.prefix + key)

            content.downloadWithDownloadType(
                .IfNewerExists,
                pinOnCompletion: pinOnCompletion,
                progressBlock: {[weak self](content: AWSContent?, progress: NSProgress?) -> Void in
                    guard self != nil else { return }
                    /* Show progress in UI. */
                },
                completionHandler: {[weak self](content: AWSContent?, data: NSData?, error: NSError?) -> Void in
                    guard self != nil else { return }
                    if let error = error {
                        // Handle Error
                        return
                    }
                    if let fileData = data {
                        let rawData = NSString(data: fileData, encoding:NSUTF8StringEncoding) as! String
                       // Do something

                    }
                    //Download Complete
                })
        }

0 个答案:

没有答案