define用法补充

define用法补充 

1)字符串的常量用法

#define SetName(name)  [[ClassName shareInstance] setName:@#name];

- (void)demo {
    // #name => "name"
    SetName(Peter);
}

2 )代码模块注入

#define Init(...)  self = [super init]; \
if(self) { \
__VA_ARGS__; \
} \
return self; \


@implementation ClassName {
    NSMutableDictionary *_cache;
}

- (id)initWithCapacity:(NSUInteger)capacity {
    //代码注入
    Init(_cache = [[NSMutableArray alloc] initWithCapacity:capacity]);
}

@end

3)加盐串动态获取

#define kLocalKey  ({ \
uint8_t bytpes[] = {0x73,0x61,0x6c,0x74,0x2d,0x6b,0x65,0x79}; \
[[NSString alloc] initWithData:[NSData dataWithBytes:bytpes length:8] encoding:NSASCIIStringEncoding]; \
}) \

#define kLocalKey  @"salt-key"

猜你喜欢

转载自blog.csdn.net/z119901214/article/details/90296641