2

This could be a completely backwards design but I wanted to know if this is possible. I just started on a project that has an existing framework called Core. Core contains protocols and when Core is loaded, it does this

for className in classNames {
    let clazz = objc_getClass(className)
...
}

Basically in the Core module it has an array of classNames that are just strings and then creates the class with objc_getClass()

All of these classes conform to CoreType protocol.

Let's say I have another smaller module, let's call it SubCore that pulls in as a dependency Core, and has its own class that conforms to CoreType.

  • Core has CoreType protocol and instantiates the classes when the SDK is loaded
  • SubCore depends on Core with Carthage and defines MyClass that conforms to CoreType

    let classNames = ["MyClass", ...] // classes that conform to CoreType that are defined in Core, and one class, MyClass, that is defined in SubCore
    for className in classNames {
        let clazz = objc_getClass(className)
        ...
    }
    

In an example app, I pull in both Core and SubCore through Carthage and try to load the SDK. When the above block is run, clazz is nil for the className "MyClass".

I'm guessing this is because MyClass is defined in SubCore and Core does not know what SubCore is. Is that correct? More importantly, is there a way to do have an interface defined in Core, have SubCore define a class that conforms to that protocol, and have it instantiated when Core is loaded like I try to above?

halfer
  • 19,824
  • 17
  • 99
  • 186
Crystal
  • 28,460
  • 62
  • 219
  • 393
  • You should use `NSClassFromString()` instead of `objc_getClass()`. Look at [this thread](https://stackoverflow.com/questions/24030814/swift-language-nsclassfromstring) for answers how to handle the namespaces. – clemens Jan 24 '18 at 05:57

0 Answers0