引用闭包中的属性需要显式的“自我".使捕获语义明确 [英] Reference to property in closure requires explicit 'self.' to make capture semantics explicit

查看:282
本文介绍了引用闭包中的属性需要显式的“自我".使捕获语义明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将HTML从Web服务加载到Web视图中时,出现此错误:

Trying to load HTML from a web service into a webview, I get this error:

在闭包中引用属性"webviewHTML"需要显式的自我".使捕获语义明确化

Reference to property 'webviewHTML' in closure requires explicit 'self.' to make capture semantics explicit

这是什么意思,如何将HTML字符串加载到Web视图中?

What does it mean, and how can I load the HTML string into my web view?

func post(url: String, params: String) {

    let url = NSURL(string: url)
    let params = String(params);
    let request = NSMutableURLRequest(URL: url!);
    request.HTTPMethod = "POST"
    request.HTTPBody = params.dataUsingEncoding(NSUTF8StringEncoding)

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in

        if error != nil {
            print("error=\(error)")
            return
        }

        var responseString : NSString!;
        responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
        webviewHTML.loadHTMLString(String(responseString), baseURL: nil)
    }
    task.resume();
}

推荐答案

在回答此问题之前,您必须知道什么是内存循环.参见

Before answering this question you must know what a memory cycle is. See Resolving Strong Reference Cycles Between Class Instances From Apple's documenation

现在您知道什么是内存周期:

Now that you know what a Memory cycle is:

该错误是Swift编译器告诉您

That error is Swift compiler telling you

嘿,NSURLSession 关闭正在尝试保留 webviewHTML 堆,并因此自己 ==>创建一个内存周期,但我没有 认为克拉克先生想在这里.想象一下,如果我们提出一个要求 永远,然后用户离开此页面.不会的 离开堆.我只是在告诉你这个,但克拉克先生必须为自己创建一个弱引用,并在闭包中使用该."

"Hey the NSURLSession closure is trying to keep webviewHTML in the heap and therefor self ==> creating a memory cycle and I don't think Mr.Clark wants that here. Imagine if we make a request which takes forever and then the user navigates away from this page. It won't leave the heap.I'm only telling you this, yet you Mr.Clark must create a weak reference to the self and use that in the closure."

我们创建(即捕获)使用[weak self]引用.我强烈建议您查看附件中有关捕获含义的链接.

We create (ie capture) the weak reference using [weak self]. I highly recommend you see the attached link on what capturing means.

有关更多信息,请参阅斯坦福课程的此刻.

For more information see this moment of Stanford course.

更正代码

func post(url: String, params: String) {

    let url = NSURL(string: url)
    let params = String(params);
    let request = NSMutableURLRequest(URL: url!);
    request.HTTPMethod = "POST"
    request.HTTPBody = params.dataUsingEncoding(NSUTF8StringEncoding)

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        [weak weakSelf self] data, response, error in

        if error != nil {
            print("error=\(error)")
            return
        }

        var responseString : NSString!;
        responseString = NSString(data: data!, encoding: NSUTF8StringEncoding) 

        weakSelf?.webviewHTML.loadHTMLString(String(responseString), baseURL: nil)
        // USED `weakSelf?` INSTEAD OF `self` 
    }
    task.resume();
}


有关详细信息,请参见我们总是在Swift的闭包内部使用[unown self]

这篇关于引用闭包中的属性需要显式的“自我".使捕获语义明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆