Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a solution for a notification problem which works well, but I'm afraid might be a bad idea.

I have a notification that needs to be handled by each instance of a class and by the class itself. To handle this, I'm registering for a notification by both the class and instances of the class. Because it's the exact same notification, I've named the class and instance method the same. This follows the standard we've set for how notification handlers are named.

Is this a bad idea? Is there some hidden got'ca that I'm missing. Will I be confusing the heck out of 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];
    }
}
like image 408
Jeffery Thomas Avatar asked Dec 06 '12 15:12

Jeffery Thomas


People also ask

What is the difference between an instance method and class method?

Instance methods need a class instance and can access the instance through self . Class methods don't need a class instance. They can't access the instance ( self ) but they have access to the class itself via cls .

What is instance method in Objective C?

Instance method are methods that are specific to particular classes. Instance methods are declared and defined followed by - (minus) symbol. Class methods can be called by class name itself . Class methods are declared and defined by using + (plus)sign .

How do you create a class method in Objective C?

The main() FunctionBankAccount *account1, *account2; account1 = [[BankAccount newAlloc] init]; account2 = [[BankAccount newAlloc] init]; Having created the two instances of the class we can then call the totalOpen method to obtain the number of instances: int count = [BankAccount totalOpen];


2 Answers

This is what code comments are for :)

There's no problem in Objective C with a class method and instance method having the same name.

I would suggest either:

  • amend your notification method name spec to handle this (and then handle the class notification with a different appropriately named method), or

  • add appropriate comment to explain what's happening for benefit of future potentially confused developers

like image 61
occulus Avatar answered Oct 24 '22 23:10

occulus


The language itself and the runtime will see no ambiguity in what you're doing. So you're safe on that front.

In terms of confusing future maintainers I guess you needn't be too concerned with silly autocomplete mistakes because it's not a method you intend to make manual calls to.

That said, I'd be tempted to move the class stuff into an artificial category. That'll not only give separation on the page but make it explicit that the class intends to respond as a separate tranche of functionality from the instance responses.

like image 26
Tommy Avatar answered Oct 24 '22 22:10

Tommy