GithubHelp home page GithubHelp logo

whycoding126 / hodor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jumperb/hodor

0.0 2.0 0.0 266 KB

Annotation support, COC (convention over configuration) support, Runtime support, and some easy categories, defines

License: MIT License

Objective-C 97.97% C++ 1.05% Ruby 0.98%

hodor's Introduction

#Hodor

This library provides some unversal features and a group of categories for UIKit and Foundation. all the categories and features are very usefull and universal, but every one is too small , so we put them together. it provides:

  • Annotation support for objc, like java annotation
  • Class Manager for COC(convention over configuration) design or construct some unconfig solution
  • extend GCD features
  • basical categories

#How to use ##How to use HClassManager

sometimes you want to get all the sub class of a baseClass, or you want to get all the imp class of a protocal. try this !

first you need register a class to a key

#define TestProRegKey @"TestProRegKey"

@protocol TestPro <NSObject>
@end


#define AClassRegKey @"AClassRegKey"
@interface A : NSObject
@end

@interface B : A
@end

@interface C : A
@end

@interface D : A
@end



@implementation B
HReg3(AClassRegKey, HRegInfo(TestProRegKey, @"some attr"))
@end

@implementation C
HReg2(AClassRegKey, @{@"attr":@"value"})
@end

@implementation D
HReg(TestProRegKey)
@end

then you can find it anywhere like this

[HClassManager scanClassForKey:AClassRegKey fetchblock:^(__unsafe_unretained Class aclass, id userInfo) {
	NSLog(@"get sub class: %@, userInfo:%@", NSStringFromClass(aclass), userInfo);
}];
[HClassManager scanClassNameForKey:TestProRegKey fetchblock:^(NSString *aclassName, id userInfo) {
	NSLog(@"get implement class: %@, userInfo:%@", aclassName, userInfo);
}];            

and there are some other register/get function for protocal specially

define HRegForProtocal(pro)
+ (id)getObjectOfProtocal:(Protocol *)protocal;

##How to use annotation category if you want to set some special attributes to your property of some class. like
@property (nonatomic, customkey) NSString *str;
and you want to do something according to this customkey. there is not a very beautiful solution , but relatively simple.

this is a header file

@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

write some annotion like this

@implementation AnnotationTestObj

ppx(str, @"a", @(1), @[@"c",@"d"], @{@"e":@(2)})
ppx(ID, @"f")
ppx(c, @"g")
ppx(p,@(2))

ppx(func1, @[@"h",@"i"])
- (void)func1 {}

ppx(func2_withb_, @{@"j":@"k"})
- (void)func2:(int)a withb:(NSString *)b {}

ppx(func3, @(5))
+ (void)func3 {}

- (void)func4 {}
@end

then you can get these annotions like this

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);
}

##How to use extend invoke feature Is your appDelegate very very large? and you want to put these code to a coherenct place. if you have an object , you can use notification to meet the requirement. but if you don't has a object, how ?

you can try use this solution: "extend invoke". it can dispatch invoke to your coherenct place.

first you need add a extend invoke

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //EI
    H_ExtendInvoke3(&application,&launchOptions);
    return YES;
}

then in another module write a category like this

@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

you can use it at all decentralize solutions.

##Other categories

Just look at the code. there are some very intersting categories

  • -[NSObject jsonString] : everything (include your custom object) could be json encode
  • methodSwizzle
  • -[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, all the property could read and write
  • UIView.userInfo you can save some context info
  • -[UIView removeAllSubViews]

##Other function and defines

  • syncAtQueue(dispatch_queue_t, ^) a safe sync dispatch method.
  • universal block define
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

hodor's People

Contributors

zhangchutian14 avatar jumperb avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.