-
objc_subclassing_restricted
定义一个
不可被继承的类
,如下代码,在@interface WBTools : NSObject
前面加上__attribute__((objc_subclassing_restricted))
即可
__attribute__((objc_subclassing_restricted))
@interface WBTools : NSObject
@end
@implementation WBTools
@end
/** ❌ Cannot subclass a class that was declared with the 'objc_subclassing_restricted' attribute */
@interface WBNetworkTools : WBTools
@end
@implementation WBNetworkTools
@end
-
objc_requires_super
NS_REQUIRES_SUPER
标识子类继承父类方法时,需要调用[super xxMethod]
方法,否则给出编译警告⚠️
@interface WBTools : NSObject
/** 写法一 */
- (void)initialize __attribute__((objc_requires_super));
/** 写法二 */
- (void)wb_initialize NS_REQUIRES_SUPER;
@end
@implementation WBTools
- (void)initialize { }
- (void)wb_initialize { }
@end
@interface WBNetworkTools : WBTools
@end
@implementation WBNetworkTools
- (void)initialize {
/** ⚠️ Method possibly missing a [super initialize] call */
}
- (void)wb_initialize {
/** ⚠️ Method possibly missing a [super wb_initialize] call */
}
@end
网友评论