简体   繁体   English

访问自身内部封闭类的返回类型

[英]Access self inside closure returning type of individual class

I would like to be able to access self in the following closure and always get the following error: Value of type '(NSObject) -> () -> MyViewController' has no member 'pushViewController' . 我希望能够在以下关闭中访问self并始终收到以下错误: Value of type '(NSObject) -> () -> MyViewController' has no member 'pushViewController' I didn't manage to find a solution yet. 我还没有找到解决方案。 Could you please help me and explain why I cannot access my current class or rather self? 您能帮我一下,解释一下为什么我不能进入我目前的班级,甚至无法进入自我吗?

//description view (try to use all the space... get length of text and adjust size accordingly)
let postDescription: PaddingLabel = {

    let entireDescView = PaddingLabel()

    //set properties of label

    //using library "ActiveLabel"
    entireDescView.enabledTypes = [.mention]
    entireDescView.mentionColor = UIColor(red: 25/255, green: 153/255, blue: 1, alpha: 1)
    entireDescView.handleMentionTap { hotel in 
        self.pushViewController(of: hotel) //cannot access self here
    }

    return entireDescView
}()

I think the problem is that closure is inside a computed property definition. 我认为问题在于闭包位于计算的属性定义内。

That definition exists before class or struct will be initiated. 该定义启动类或结构之前就已经存在。

You could fix it declaring postDescription as lazy var 您可以修复它,将postDescription声明为lazy var

public class User
{
    let postDescription: String = {

        return self.saludar() // Error. use of unresolved identifier 'self'
    }()

    internal func saludar() -> String
    {
        return "testing greeeting..."
    }
}

With lazy var ... 随着lazy var ...

public class User
{
    lazy var postDescription: String = {

        return self.saludar() // It's OK
    }()

    internal func saludar() -> String
    {
        return "testing greeeting..."
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM