Extension of a nested type in Swift

前端 未结 2 757
醉话见心
醉话见心 2020-12-08 13:37

I have a main class, also providing a namespace:

class A {
}

and a nested class added via an extension (all for the sake of using separate

相关标签:
2条回答
  • 2020-12-08 14:21

    this works in my playground, as expected

    class A {
    }
    extension A {
        class B {
        }
    }
    extension A.B {
        func foo() {
            print("print from extension A.B")
        }
    }
    let ab = A.B()
    ab.foo()    // print from extension A.B
    
    0 讨论(0)
  • 2020-12-08 14:23

    It seems like this problem is related to SR-631. I've encountered similar a issue, I guess the complier is trying to process the file where you extend the nested class before the one where it's defined. Therefore you have this error saying that that A has no member B.

    The solution I've found is to go to your target settings, open Build Phases.

    There, in Compile Sources section you should put the file where you define the nested class above files where you extend it.

    Update

    The fix will be shipping with Xcode 10.2

    0 讨论(0)
提交回复
热议问题