1.objc_subclassing_restricted
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
NS_ASSUME_NONNULL_BEGIN
__attribute__((objc_subclassing_restricted))//禁止该类被继承
@interface AAA : UIView
-(void)mustUseMethod;
@end
NS_ASSUME_NONNULL_END
如果某类继承了AAA这个类,那么就会报错
#import "AAA.h"
NS_ASSUME_NONNULL_BEGIN
@interface BBB : AAA Cannot subclass a class that was declared with the 'objc_subclassing_restricted' attribute
@end
2.objc_requires_super
objc_requires_super 在父类的方法后面添加,那么子类调用该方法必须实现[super thisMethod],否则会黄色警告
@interface Father : NSObject
- (void)mustUseMethod __attribute__((objc_requires_super));
@end
@interface Child : TestObject
@end
@implementation BBB
-(void)mustUseMethod{
//[super mustUseMethod];
警告信息:(不报错)
Method possibly missing a [super mustUseMethod] call
}
@end
3.constructor / destructor
__attribute__((constructor)) static void beforeMain() {
NSLog(@"before main");
}
__attribute__((destructor)) static void afterMain() {
NSLog(@"after main");
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@"execute main");
}
return 0;
}
执行结果:
debug-objc[23391:1143291] before main
debug-objc[23391:1143291] execute main
debug-objc[23391:1143291] after main
可以在constructor后边添加设置优先级
__attribute__((constructor(101))) static void beforeMain() { NSLog(@"before main");
}
__attribute__((constructor(100))) static void beforeMain1() { NSLog(@"before main1");
}
__attribute__((destructor)) static void afterMain() { NSLog(@"after main");
}
执行结果:
2019-10-12 15:48:24.015908+0800 Food[4561:144694] before main1
2019-10-12 15:48:24.016491+0800 Food[4561:144694] before main
2019-10-12 15:48:24.017145+0800 Food[4561:144694] execute main
4.overloadable
可以允许同名函数存在
__attribute__((overloadable)) void testMethod(int age) {NSLog(@"%@", @(age));};
__attribute__((overloadable)) void testMethod(NSString *name) {NSLog(@"%@", name);};
__attribute__((overloadable)) void testMethod(BOOL gender) {NSLog(@"%@", @(gender));};
int main(int argc, char * argv[]) {
@autoreleasepool {
testMethod(18);
testMethod(@"lxz");
testMethod(YES);
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
执行结果:
2019-10-12 16:01:04.491366+0800 Food[4724:150619] 18
2019-10-12 16:01:04.491510+0800 Food[4724:150619] lxz
2019-10-12 16:01:04.491632+0800 Food[4724:150619] 1
5.objc_runtime_name
objc_runtime_name属性可以在编译时,将Class或者Protocol指定为另一个名字
这个属性可以用来做代码混淆,例如写一个宏定义,这个宏定义内部实现混淆逻辑。例如通过MD5对object做混淆,32位的混淆结果就是497031794414a552435f90151ac3b54b,谁能看出来这个是什么类。
__attribute__((objc_runtime_name("TestObject")))
@interface AAA : NSObject
@end
int main(int argc, char * argv[]) {
@autoreleasepool {
NSLog(@"--%@",NSStringFromClass([AAA class]));
NSLog(@"----%@",NSClassFromString(@"TestObject"));
}
}
执行结果:
2019-10-12 16:10:24.414452+0800 Food[4917:156444] --TestObject
2019-10-12 16:10:24.416007+0800 Food[4917:156444] ----TestObject
6.cleanup
通过cleanup属性,可以指定给一个变量,当变量释放之前执行一个函数。指定的函数执行时间,是在dealloc之前。在指定的函数中,可以传入一个形参,参数就是cleanup修饰的变量,形参是一个地址。
static void releaseBefore(NSObject **object) {
NSLog(@"AAA-------%@", *object);
}
int main(int argc, char * argv[]) {
@autoreleasepool {
{
AAA *object __attribute__((cleanup(releaseBefore))) = [AAA new];
}
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
执行结果:
2019-10-12 16:20:24.230282+0800 Food[5057:162194] AAA-------<AAA: 0x6000021c81d0>
2019-10-12 16:20:24.230633+0800 Food[5057:162194] AAA-dealloc
7.unused
如果某个变量未使用,会提示unused xxx,可以通过unused消除这个警告
int main(int argc, char * argv[]) {
@autoreleasepool {
AAA *object __attribute__((unused)) = [AAA new];
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}