我有一个解决通知问题的解决方案很好,但我担心这可能是一个坏主意.
我有一个通知,需要由类的每个实例和类本身来处理.为了解决这个问题,我正在注册类的类和实例的通知.因为它是完全相同的通知,所以我将类和实例方法命名为相同.这遵循我们为通知处理程序的命名方式设置的标准.
这是一个坏主意吗?是否有一些我失踪的隐藏的东西.我是否会混淆未来的开发者?
+ (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]; } }@H_301_7@