1.NS_DESIGNATED_INITIALIZER
- 方法后面带有 NS_DESIGNATED_INITIALIZER 时, 为特定构造方法
- 注意:子类如果重写了父类的特定构造方法, 那么必须使用super调用父类的特定构造方法
- 警告信息:
- Designated initializer missing a ‘super‘ call to a designated initializer of the super class
- 例子
- (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.titleLabel.font = [UIFont systemFontOfSize:16]; // 文字颜色 [self setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal]; [self setTitleColor:[UIColor redColor] forState:UIControlStateSelected]; } return self; }
2.NS_REQUIRES_SUPER
- 方法后面带有 NS_REQUIRES_SUPER , 则必须先调用父类的方法
- 警告信息:
- Method possibly missing a [super run] call
@interface XMGTest : NSObject - (void)run NS_REQUIRES_SUPER; @end @interface XMGSecondTest : XMGTest @end @implementation XMGSecondTest - (void)run { // 不调用, 则会有警告信信 //[super run]; } @end
时间: 2024-11-07 06:37:09