在Objective-C中具有相同名称的类方法和实例方法 [英] Class method and instance method with the same name in Objective-C

查看:206
本文介绍了在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.

这是一个坏主意吗?有没有一些隐藏的got'ca,我失踪。

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


推荐答案

是什么代码注释:)

Objective C没有问题,类方法和实例方法具有相同的名称。

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

我会建议:


  • 修改您的通知方法名称规范处理此类(然后使用不同的适当命名的方法处理类通知),或

  • 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

这篇关于在Objective-C中具有相同名称的类方法和实例方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆