Sunday, November 1, 2026
HomeiOS Developmentios - Swizzling + methodology to log every class intantiation

ios – Swizzling +[NSObject initialize] methodology to log every class intantiation

[ad_1]

Mainly, I need to print every time a category object is instantiated. The next code reveals the intent.

@interface NSObject (ILogger)
+ (void)initialize;
@finish

@implementation NSObject (ILogger)
+ (void)initialize
{
    NSLog(@"Initializing %s", class_getName([self class]));
}
@finish

This doesn’t work as a result of NSObject already has a +initialize methodology so this method ends in undefined habits. The compiler additionally warns in regards to the problem: warning: class is implementing a technique which will even be applied by its main class

One thought can be to by some means swizzle +[NSObject initialize] and do the logging. How do I do this safely?

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments