mercredi 1 juin 2016

Swift: NSClassFromString from dependencies

I'm having this problem getting the reflection to work from external frameworks as dependencies.

So let say I had two projects, one is a sample app project

SampleApp.xcodeproj
@obj protocol Extension : NSObjectProtocol {
    //..Some definitions
}

And another with the framework project that the sample app will be depended on and is a submodule of the sample app project.

Framework.xcodeproj
class SampleClass : NSObject, Extension {
   //some definitions
}

In this framework, Extension.swift file, where the Extension protocol is defined, is included in the build as a reference

Now here's the problem on this code

if let customExtension = NSClassFromString("Framework.SampleClass") as? NSObject.Type {
    let extensionInstance = customExtension.init()
    if extensionInstance is Extension { //The problem here, it returned false at this point
        print("It is an Extension")
    } else {
        print("It is not an Extension")
    }
}

I noticed that the Extension protocol defined in the Framework project was defined as Framework.Extension instead of SampleApp.Extension. How would I make it so the SampleClass class is defined as SampleApp.Extension instead of Framework.Extension?





Aucun commentaire:

Enregistrer un commentaire