Warm tip: This article is reproduced from stackoverflow.com, please click
macos objective-c private

How to instantiate private / hidden Objective C framework class?

发布于 2020-03-27 10:18:24

If you perform a class-dump of AppKit it reveals multiple private / hidden classes defined in that framework.

One example is the following class:

__attribute__((visibility("hidden")))
@interface _NSFullScreenWindow : NSWindow
{
}

- (BOOL)canBecomeKeyWindow;

@end

How do you create an instance of a class that is intended to be private / hidden in Objective-C?

I know that private / hidden classes are not meant to be accessed, so no need for moral lessons.

Questioner
Robin Andersson
Viewed
105
Robin Andersson 2019-07-04 15:34

You can access the class by name and instantiate it as following:

Class winClass = NSClassFromString(@"_NSFullScreenWindow");
id win = [winClass new];

Win will be a pointer to an instance of _NSFullScreenWindow after running that code.