Class method and instance method with the same name in Objective-C

I have a solution to the notification problem that works well, but I'm afraid it will be a bad idea.

I have a notification that should be handled by each instance of the class and the class itself. To handle this, I am registering a notification for the class and class instances. Since this is the same notification, I called the class and instance method the same. This follows the standard we set for what the notification handlers are called.

It is a bad idea? There are some hidden got'ca that I miss. Will I be confused with future developers?

+ (void)initialize { if (self == [SICOHTTPClient class]) { [[self notificationCenter] addObserver:self selector:@selector(authorizationDidChangeNotification:) name:SICOJSONRequestOperationAuthorizationDidChangeNotification object:nil]; } } - (id)initWithBaseURL:(NSURL *)url { self = [super initWithBaseURL:url]; if (self) { self.parameterEncoding = AFJSONParameterEncoding; [self registerHTTPOperationClass:[SICOJSONRequestOperation class]]; [self setDefaultHeader:@"Accept" value:@"application/json"]; if ([[self class] defaultAuthorization]) [self setDefaultHeader:@"Authorization" value:[[self class] defaultAuthorization]]; [[[self class] notificationCenter] addObserver:self selector:@selector(authorizationDidChangeNotification:) name:SICOJSONRequestOperationAuthorizationDidChangeNotification object:nil]; } return self; } - (void)dealloc { [[[self class] notificationCenter] removeObserver:self name:SICOJSONRequestOperationAuthorizationDidChangeNotification object:nil]; } #pragma mark Notifications - (void)authorizationDidChangeNotification:(NSNotification *)notification { NSString *authorization = notification.userInfo[SICOJSONRequestOperationAuthorizationKey]; if ([authorization isKindOfClass:[NSString class]]) { [self setDefaultHeader:@"Authorization" value:authorization]; } else { [self clearAuthorizationHeader]; } } + (void)authorizationDidChangeNotification:(NSNotification *)notification { NSString *authorization = notification.userInfo[SICOJSONRequestOperationAuthorizationKey]; if ([authorization isKindOfClass:[NSString class]]) { [self setDefaultAuthorization:authorization]; } else { [self setDefaultAuthorization:nil]; } } 
+7
source share
2 answers

Here are some code comments for :)

In Objective-C, there is no problem with the class method and instance method with the same name.

I would suggest either:

  • change your specification specification method name to handle this (and then process the class notification using another method with the appropriate name) or

  • Add a comment to explain what’s happening to future potentially confusing developers.

+3
source

The language itself and the runtime will not see the ambiguity in what you do. So, you are safe on this front.

In terms of obfuscating future maintainers, I think you don’t have to worry too much about stupid autocomplete errors, because this is not the method that you intend to do with manual calls.

However, I will be tempted to move the class to an artificial category. This will not only give a separation on the page, but also make it clear that the class intends to respond as a separate tranche of functionality from the responses of the instance.

+1
source

All Articles