4

I have a string with the name of a class and I'm trying to get the framework name where the class belongs.

Things that I have tried:

  1. bundleForClass
Class *myClass = NSClassFromString(@"ClassName");
[NSBundle bundleForClass:myClass];

But I can't seem to make it work with relocatable / static mach-o type.

  1. Adding new method on NSObject using categories
// This is on NSObject+ModuleID.h
@interface NSObject (ModuleID)
- (NSString *) moduleIdentifier;
@end

// This is on NSObject+ModuleID.m
@implementation NSObject (ModuleID)
- (NSString *) moduleIdentifier { return @"Framework Name"; }
@end

Class *myClass = NSClassFromString(@"ClassName");
[myClass moduleIdentifier];

This doesn't work since the project include more than 1 framework.

  1. Adding a new string property containing the framework name to each class in the frameworks
-(NSString *)moduleIdentifier
{ return @"Framework Name"; }

This is not feasible considering the number of the classes easily exceeds a hundred.

Is there any way to do this? Any input would be appreciated, thanks!

702
  • 41
  • 1
  • So conceptually you want something that `lldb` https://stackoverflow.com/a/40120375/5329717 yields here? – Kamil.S Apr 19 '20 at 06:48

1 Answers1

0

Try

-(NSString *)moduleIdentifier
{
    return [NSBundle bundleForClass:self.class].bundleIdentifier;
}
Cy-4AH
  • 4,370
  • 2
  • 15
  • 22
  • 1
    Although this code might solve the problem, a good answer should also explain **what** the code does and **how** it helps. – BDL Apr 17 '20 at 11:21
  • Unfortunately this doesn't work as bundleForClass always returns the mainBundle instead of the framework when using relocatable / static mach-o type – 702 Apr 18 '20 at 09:03