我可以检测到:"Does class overload method of base class"?
Can I detect: "Does class overload method of base class"?
我的 class
中有两种方法
- (void)configureWithDictionary:(NSDictionary*)dictionary;
- (void)configureWithDictionary:(NSDictionary*)dictionary withOptions:(XWTreeItemConvertationToNSDictionaryOption*)options;
而且我已经实现了它们。所以!解决方案如:"Just add NSAssert(NO, @"你必须覆盖此方法")" 无济于事 =(
- (void)configureWithDictionary:(NSDictionary*)dictionary withOptions:(XWTreeItemConvertationToNSDictionaryOption*)options;
{
NSAssert(NO, @"You mast override this method"
}
因为我那边有一些代码。并且需要在重载方法中写 [super configureWithDictionary:dictionary withOptions:options];
。
每个人都可以使用这种方法。我两者都需要!但是
如果一些开发人员会超载 -[MYClass configureWithDictionary:]
它可以 "work incorrect"。只是因为这个方法没有任何时候调用。所以我需要在控制台中写一些东西。喜欢:"Please overload method: -[MYClass configureWithDictionary:withOptions:]
"。我只想用这种方法处理一次:
+ (void)initialize
{
if (self == [self class]) {
}
}
但我找不到任何解决方案(在documentation/google/Whosebug)。并且无法处理:"Does developer overload method of base class".
可能有更好的解决方案。但我认为它应该是最好的。如果您有其他想法。请在下面写下 =)
我只找到了方法:+[NSObject instancesRespondToSelector]
当然我知道 -[NSObject respondsToSelector:]
但正如你所知,它总是 return 是的。我需要几乎相同,但对于当前 class 忽略基数。
PS。无论如何感谢您的关注。 Link 文档或一些文章会很有帮助。
可能这不完全是您要问的,但是当我需要确保子 类 重载某些必需的方法时,我会这样做:
@protocol SomeClassRequiredOverload
- (void) someMethodThatShouldBeOverloaded;
@end
@interface _SomeClass
@end
typedef _SomeClass<SomeClassRequiredOverload> SomeClass;
我自己找到了解决方案,我认为它可以帮助社区。所以 3 个简单的步骤。
第 1 步:使用方法创建类别表单 NSObject
+ (NSArray*)methodNamesForClass_WithoutBaseMethodsClasses
{
unsigned int methodCount = 0;
Method *methods = class_copyMethodList(self, &methodCount);
NSMutableArray *array = [NSMutableArray arrayWithCapacity:methodCount];
for (unsigned int i = 0; i < methodCount; i++) {
Method method = methods[i];
[array addObject:[NSString stringWithFormat:@"%s", sel_getName(method_getName(method))]];
}
free(methods);
return [array copy];
}
第 2 步:检查您是否 class 重载了某些方法:
[[self methodNamesForClass_WithoutBaseMethodsClasses] containsObject:NSStringFromSelector(@selector(configureWithDictionary:))]
第 3 步:在 + (void)initialize
中检查您需要的所有内容。它调用一次 class(因此不会有很多 CPU 时间)。而且它只需要开发人员。所以添加 #ifdef DEBUG
指令
+ (void)initialize
{
if (self == [self class]) {
#ifdef DEBUG
if ([[self methodNamesForClass_WithoutBaseMethodsClasses] containsObject:NSStringFromSelector(@selector(configureWithDictionary:))] && ![[self methodNamesForClass_WithoutBaseMethodsClasses] containsObject:NSStringFromSelector(@selector(configureWithDictionary:withOptions:))]) {
NSAssert(NO, @"Please override method: -[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(@selector(configureWithDictionary:withOptions:)));
}
#endif
}
}
胜利!
我的 class
中有两种方法- (void)configureWithDictionary:(NSDictionary*)dictionary;
- (void)configureWithDictionary:(NSDictionary*)dictionary withOptions:(XWTreeItemConvertationToNSDictionaryOption*)options;
而且我已经实现了它们。所以!解决方案如:"Just add NSAssert(NO, @"你必须覆盖此方法")" 无济于事 =(
- (void)configureWithDictionary:(NSDictionary*)dictionary withOptions:(XWTreeItemConvertationToNSDictionaryOption*)options;
{
NSAssert(NO, @"You mast override this method"
}
因为我那边有一些代码。并且需要在重载方法中写 [super configureWithDictionary:dictionary withOptions:options];
。
每个人都可以使用这种方法。我两者都需要!但是
如果一些开发人员会超载 -[MYClass configureWithDictionary:]
它可以 "work incorrect"。只是因为这个方法没有任何时候调用。所以我需要在控制台中写一些东西。喜欢:"Please overload method: -[MYClass configureWithDictionary:withOptions:]
"。我只想用这种方法处理一次:
+ (void)initialize
{
if (self == [self class]) {
}
}
但我找不到任何解决方案(在documentation/google/Whosebug)。并且无法处理:"Does developer overload method of base class".
可能有更好的解决方案。但我认为它应该是最好的。如果您有其他想法。请在下面写下 =)
我只找到了方法:+[NSObject instancesRespondToSelector]
当然我知道 -[NSObject respondsToSelector:]
但正如你所知,它总是 return 是的。我需要几乎相同,但对于当前 class 忽略基数。
PS。无论如何感谢您的关注。 Link 文档或一些文章会很有帮助。
可能这不完全是您要问的,但是当我需要确保子 类 重载某些必需的方法时,我会这样做:
@protocol SomeClassRequiredOverload
- (void) someMethodThatShouldBeOverloaded;
@end
@interface _SomeClass
@end
typedef _SomeClass<SomeClassRequiredOverload> SomeClass;
我自己找到了解决方案,我认为它可以帮助社区。所以 3 个简单的步骤。
第 1 步:使用方法创建类别表单 NSObject
+ (NSArray*)methodNamesForClass_WithoutBaseMethodsClasses
{
unsigned int methodCount = 0;
Method *methods = class_copyMethodList(self, &methodCount);
NSMutableArray *array = [NSMutableArray arrayWithCapacity:methodCount];
for (unsigned int i = 0; i < methodCount; i++) {
Method method = methods[i];
[array addObject:[NSString stringWithFormat:@"%s", sel_getName(method_getName(method))]];
}
free(methods);
return [array copy];
}
第 2 步:检查您是否 class 重载了某些方法:
[[self methodNamesForClass_WithoutBaseMethodsClasses] containsObject:NSStringFromSelector(@selector(configureWithDictionary:))]
第 3 步:在 + (void)initialize
中检查您需要的所有内容。它调用一次 class(因此不会有很多 CPU 时间)。而且它只需要开发人员。所以添加 #ifdef DEBUG
指令
+ (void)initialize
{
if (self == [self class]) {
#ifdef DEBUG
if ([[self methodNamesForClass_WithoutBaseMethodsClasses] containsObject:NSStringFromSelector(@selector(configureWithDictionary:))] && ![[self methodNamesForClass_WithoutBaseMethodsClasses] containsObject:NSStringFromSelector(@selector(configureWithDictionary:withOptions:))]) {
NSAssert(NO, @"Please override method: -[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(@selector(configureWithDictionary:withOptions:)));
}
#endif
}
}
胜利!