"instance member cannot be used on type" error on Swift 4 with nested classes

Viewed 7361

I have a class with a nested class. I'm trying to access variables of the outer class from within the nested class:

class Thing{
    var name : String?
    var t = Thong()

    class Thong{
        func printMe(){
            print(name) // error: instance member 'name' cannot be used on type 'Thing'
        }
    }

}

This however, gives me the following error:

instance member 'name' cannot be used on type 'Thing'

Is there an elegant way to circumvent this? I was hoping for nested classes to capture the lexical scope, as closures do.

Thanks

2 Answers

Try passing in the variable instead of trying to use it directly.

class Thing{
    var name : String?
    var t = Thong()

    class Thong{
        func printMe(name: String){
            print(name) 
        }
    }
}
Related