Hodor
此库提供了一些通用特性和一组UIKit和Foundation的分类。所有的分类和功能都非常实用且通用,但每个都非常小,所以我们把它们放在一起。它提供
- Objective-C的注释支持,例如 Java注解
- COC(约定优于配置)设计中的类管理器或构建一些无配置解决方案
- 扩展GCD功能
- 基本分类
如何使用
如何使用HClassManager
有时,您想要获取一个基类的所有子类,或者您想要获取一个协议的所有实现类,试试这个!
首先,将一个类注册到一个键上
@implementation ClassA
//tell HClassManager Im a IMP of WProtocol
HRegForProtocal(WProtocal)
@end
@implementation ClassB
//tell HClassManager Im a sub class of ClassA, and im a IMP of XProtocol, then I has some other property
HReg3(ClassARegKey, HProtocalRegKey(XProtocol), HRegInfo(@"somekey", @"userinfo"))
@end
@implementation ClassC
HReg2(ClassARegKey, @{@"attr":@"value"})
@end
@implementation ClassD
HRegForProtocal(XProtocol)
@end
@implementation ClassE
//tell HClassManager Im a IMP of YProtocol, and use "shareInstance" to get an object
HRegForProtocalAsSingleton(YProtocol, @"shareInstance")
+ (instancetype)shareInstance
{
static dispatch_once_t pred;
static ClassE *o = nil;
dispatch_once(&pred, ^{ o = [[self alloc] init]; });
return o;
}
- (void)testFun
{
NSLog(@"testFun");
}
@end
然后您可以在任何地方找到它,如下所示
//search sub class
[HClassManager scanClassForKey:ClassARegKey fetchblock:^(__unsafe_unretained Class aclass, id userInfo) {
NSLog(@"get sub class: %@, userInfo:%@", NSStringFromClass(aclass), userInfo);
}];
//search implement of protocal
NSString *key = HProtocalRegKey(XProtocol);
[HClassManager scanClassNameForKey:key fetchblock:^(NSString *aclassName, id userInfo) {
NSLog(@"get implement class: %@, userInfo:%@", aclassName, userInfo);
}];
//directly use protocal implment with dependence
[HProtocalInstance(YProtocol) testFun];
此外,还有一些专门用于协议的注册/获取功能
define HRegForProtocal(pro)
+ (id)getObjectOfProtocal:(Protocol *)protocal;
如果您想通过IOC/DI原则构建项目,请尝试“HRegForProtocal()”和“-[NSObject dependenceInset]”
只需两行代码即可
如何使用注释类别
如果你想为某个类的属性设置一些特殊的属性。例如:
@property (nonatomic, customkey
) NSString *str;
并且你想根据这个自定义键做些事情。虽然没有非常漂亮的方法,但相对简单。
这是一个头文件
@interface AnnotationTestObj : NSNumber
@property (nonatomic) NSString *str;
@property (nonatomic) id ID;
@property (nonatomic) char c;
@property (nonatomic) int *p;
@property (nonatomic) BOOL b;
- (void)func1;
- (void)func2:(int)a withb:(NSString *)b;
+ (void)func3;
- (void)func4;
@end
编写一些注释如下:
@implementation AnnotationTestObj
ppx(str, @"a", @(1), @[@"c",@"d"], @{@"e":@(2)})
ppx(ID, @"f")
ppx(c, @"g")
ppx(p,@(2))
fpx(func1, @[@"h",@"i"])
- (void)func1 {}
fpx(func2_withb_, @{@"j":@"k"})
- (void)func2:(int)a withb:(NSString *)b {}
fpx(func3, @(5))
+ (void)func3 {}
- (void)func4 {}
@end
然后你可以这样获取这些注释:
Class theClass = [AnnotationTestObj class];
while (theClass != [NSObject class]) {
NSArray *ppList = [NSObject ppListOfClass:theClass];
for (NSString *ppName in ppList)
{
NSArray *ants = [theClass annotations:ppName];
if (ants) NSLog(@"property:%@ annotations:%@", ppName, ants);
}
NSArray *instanceMethods = [NSObject hInstanceMethodNames:theClass];
NSArray *classMethods = [NSObject hClassMethodNames:theClass];
NSArray *methods = [instanceMethods arrayByAddingObjectsFromArray:classMethods];
for (NSString *name in methods)
{
NSArray *ants = [theClass annotations:hFormateAnnotationName(name)];
if (ants) NSLog(@"function:%@ annotations:%@", name, ants);
}
theClass = class_getSuperclass(theClass);
}
如何使用扩展调用功能
你的AppDelegate非常大吗?你想把这些代码放到一个连贯的地方。如果你有一个对象,你可以使用通知来满足需求。但是如果没有对象呢?
你可以尝试使用这种解决方案:“扩展调用”。它可以将调用分发给你的连贯位置。
首先,你需要添加一个扩展调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//EI
H_ExtendInvoke3(&application,&launchOptions);
return YES;
}
然后在另一个模块中编写一个类别如下:
@implementation AppDelegate (moduleA)
- (BOOL)moduleA_application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"## moduleA extend 'didFinishLaunchingWithOptions'");
return YES;
}
@end
@implementation AppDelegate (moduleB)
- (BOOL)moduleB_application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"## moduleB extent 'didFinishLaunchingWithOptions'");
return YES;
}
@end
你可以在所有分散的解决方案中使用它。
其他类别
看看代码。有一些非常有趣的类别
-[NSObject jsonString]
:所有内容(包括你的自定义对象)都可以进行JSON编码方法Swizzle
-[UIApplication getKeyWindowRootController]
-[UIButton hSetColor:font:title:]
-[UIColor colorWithHex:]
,-[UIColor random]
-[UILabel hSetColor:font:text:]
UIView.x UIView.y UIView.width, UIView.height, UIView.xmax UIView.ymax
,所有属性都可以读写UIView.userInfo
你可以保存一些上下文信息-[UIView removeAllSubViews]
其他函数和定义
syncAtQueue(dispatch_queue_t, ^)
一个安全的同步分派方法。通用块定义
typedef void (^min_callback)();
typedef void (^callback)(id sender, id data);
typedef void (^callback2)(id sender, id data, id data2);
typedef void (^simple_callback)(id sender);
typedef void (^fail_callback)(id sender, NSError *error);
typedef id (^returnback)(id sender, id data);
typedef void (^finish_callback)(id sender, id data, NSError *error);
- @weakify, @strongify