mercredi 6 octobre 2021

ios load objective c class vs swift class in reflection using objective c

I'm trying to load a class using reflection in objective c and getting different results for a swift class vs an objective c class

What I'm seeing is that using NSClassFromString, for an objective c class i need to give as input the class name only and for a swift class I need to give as input the bundle id as prefix as well

I would like to know why that is and if there's a way to use one code for both platforms that works using the same input

Examples below

e.g.

objective c class - A

swift class - B

the bundle id of the project is Test

Class  test1 = NSClassFromString(@"A"); // works

Class  test2 = NSClassFromString(@"Test.A"); // doesn't work

Class  test3 = NSClassFromString(@"B"); // doesn't work

Class  test4 = NSClassFromString(@"Test.B"); // works

I tried to do the same thing in swift and got the same problem

var  test1 = NSClassFromString("A") as? A.Type // works

var  test2 = NSClassFromString("Test.A") as? A.Type // doesn't work

var  test3 = NSClassFromString("B") as? B.Type // doesn't work

var  test4 = NSClassFromString("Test.B") as? B.Type // works

Guy





Aucun commentaire:

Enregistrer un commentaire