简体   繁体   English

Objective-C中具有相同名称的类方法和实例方法

[英]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];
    }
}

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. Objective C中没有问题,类方法和实例方法具有相同的名称。

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 添加适当的评论来解释为未来可能混淆的开发人员带来的好处

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. 这不仅会在页面上进行分离,而且会明确表示该类打算作为实例响应中的单独一部分功能进行响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM