2

I'm trying to define a Swift class that has a recursive function that returns the names of all the variables in the class. It works for printing it's variables, but when I use this I'll be using it as a base class for my models, and I may have multiple layers of inheritance. I want the method to return an array of all variable names on the current instance, as well as the names of any variables on any super classes, until we reach the base Cool class.

class Cool:NSObject {       
    func doStuff() -> [String] {
        var values = [String]()
        let mirrorTypes = reflect(self)
        for i in 0 ..< mirrorTypes.count {
            let (name, type) = mirrorTypes[i]
            if let superCool = super as! Cool while name == "super" {
                values += superCool.doStuff()   
            }
        }
        return values
    }   
}

The problem is in:

if let superCool = super as! Cool while name == "super" {

It causes an Expected '.' or '[' after super compiler error.

jamone
  • 17,253
  • 17
  • 63
  • 98
  • Since you are inheriting from NSOBject can't you use "iskindof" like here: http://stackoverflow.com/questions/24019707/using-iskindofclass-with-swift and do simple introspection? – Jeef Apr 22 '15 at 17:15
  • I can use `isKindOf` to detect the type of super, I still have the problem of calling `super.doStuff()`. Swift doesn't recognize that super has a `doStuff()` function. Because if the instance is just a `Cool` then it doesn't have that function. So I still need to cast super to a `Cool` instance first. – jamone Apr 22 '15 at 17:33
  • Now that i look at it again it seems like super isn't actually a reference to the object per-say because wanting a dot means its expecting super to have some method you are calling no? Pointer to super is still a pointer to this is it not? – Jeef Apr 22 '15 at 17:43

0 Answers0