Search code examples
iosswiftreflectioncocoapodsmirror

How to use Mirror to access externally defined class?


I'm trying to Mirror this class:

final class DeckPresentationController {
    ...
}

defined by the DeckTransition CocoaPod, so I can change some var, but the mirror didn't reflect anything to me:

import DeckTransition

let mirror = Mirror(reflecting: DeckPresentationController())
print("children: \(mirror.children.first)")
print("count: \(mirror.children.count) ")

gives this compiling error:

unresolved identifier DeckPresentationController

Any ideas how to do it?


Solution

  • You are trying to access an internal class declared in an external module. As such, using Mirror won't get you there as well — you can't hack Swift access control using reflection like that ;)

    As a quick hack, change the class declaration from:

    final class DeckPresentationController
    

    to:

    open class DeckPresentationController
    

    and then access/override the desired properties directly — and forget about that hack-ish Mirror of yours ;)

    By the way, if your edit might be useful to others as well, consider submitting a pull request to the original CocoaPod author.