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
you could do something like this
class Thing{
var name : String = "hello world"
var t = Thong()
init() {
t.thing = self
t.printMe()
}
class Thong{
weak var thing: Thing!
func printMe(){
print(thing.name)
}
}
}
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)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With