samedi 27 juin 2015

NSClassFromString with String coming from Plist

I'm trying to instanciate a class in Swift from a String, ok it can sound weird but the ClassName comes from a Plist.

I'm using NSClassFromString but it doesn't work with the Plist String, I think that I'm close to this question: NSClassFromString works for string literals, returns nil for constructed strings

But I can't use the solution as I can't know the possible classname coming from the Plist

The weird behavior is that if I create the String manually in the code, with the same value then it works as expected... it's like the converted String from the Plist is not the real exact same thing as a new String.

There is the code

let className: String = definition["class"] as! String
let classNameManually: String = "AppName.ServiceTest"
let aClass = NSClassFromString(className) as! NSObject.Type
let instanceObject = aClass()

It works with classNameManually but not with className

Then I put a breakpoint and I have identified a slighly difference

DebugView

Do you have a magic answer ?

Thank you!

Aucun commentaire:

Enregistrer un commentaire