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

swift – 自我捕获在嵌套函数中

发布时间:2020-12-14 05:36:57 所属栏目:百科 来源:网络整理
导读:关闭我通常将[weak self]附加到我的捕获列表中,然后对自己执行一个空的检查: func myInstanceMethod(){ let myClosure = { [weak self] (result : Bool) in if let this = self { this.anotherInstanceMethod() } } functionExpectingClosure(myClosure)}
关闭我通常将[weak self]附加到我的捕获列表中,然后对自己执行一个空的检查:
func myInstanceMethod()
{
    let myClosure =
    {
       [weak self] (result : Bool) in
       if let this = self
       { 
           this.anotherInstanceMethod()
       }
    }   

    functionExpectingClosure(myClosure)
}

如果我使用嵌套函数代替闭包(或者甚至是必要的检查),或者使用这样的嵌套函数甚至是很好的做法,那么我如何执行自检的空检查,即

func myInstanceMethod()
{
    func nestedFunction(result : Bool)
    {
        anotherInstanceMethod()
    }

    functionExpectingClosure(nestedFunction)
}
不幸的是,只有Closures具有“弱自我”的“捕获列表”功能.对于嵌套函数,必须使用正常的弱或非唯一变量.
func myInstanceMethod() {
    weak var _self = self
    func nestedFunction(result : Bool) {
        _self?.anotherInstanceMethod()
    }

    functionExpectingClosure(nestedFunction)
}

(编辑:李大同)

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

    推荐文章
      热点阅读