Search code examples
swiftswift3extension-methodsswift-extensions

Extending a struct which is inside another struct


I'm trying to extend a struct that is already inside a struct. When I write the following code, I get

declaration is only valid at file scope.

struct A {
    struct AA {

    }
}

extension A {
    extension AA {

    }
}

Is it invalid to write an extension inside an extension?


Solution

  • It seems like the only way to do that is:

    extension A.AA
    {
       func test()
       {
          print("Test")
       }
    }
    

    It just worked in my playground