GithubHelp home page GithubHelp logo

chenxing640 / dyfruntimeprovider Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 163 KB

Wraps the runtime, and can quickly use for the transformation of the dictionary and model, archiving and unarchiving, adding a method, exchanging two methods, replacing a method, and getting all the variable names, property names and method names of a class.

License: Other

Objective-C 94.48% Ruby 5.52%
runtime runtime-provider runtime-application runtime-api ios objective-c objc-runtime runtime-wrapper

dyfruntimeprovider's Introduction

English Vision | 中文版

DYFRuntimeProvider

DYFRuntimeProvider wraps the runtime, and provides some common usages(Swift Version).

License MIT  CocoaPods Version  CocoaPods Platform 

Group (ID:614799921)

Installation

Using CocoaPods:

pod 'DYFRuntimeProvider'

Or

pod 'DYFRuntimeProvider', '~> 2.1.0'

Usage

Add #import "DYFRuntimeProvider.h" to your source code.

Gets all the method names of a class

1. Gets all method names of an instance of a class

NSArray *instMethods = [DYFRuntimeProvider getMethodListWithClass:UITableView.class];
NSLog(@"========instMethods: %@", instMethods);

2. Gets all class method names of a class

NSArray *clsMethods = [DYFRuntimeProvider getClassMethodListWithClass:UIView.class];
NSLog(@"========clsMethods: %@", clsMethods);

Gets all variable names of a class

NSArray *ivars = [DYFRuntimeProvider getIvarListWithClass:UIButton.class];
NSLog(@"========ivars: %@", ivars);

Gets all the property names of a class

NSArray *properties = [DYFRuntimeProvider getPropertyListWithClass:UIButton.class];
NSLog(@"========properties: %@", properties);

Take this class as an example. e.g.:

@interface Teacher : NSObject
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *address;

- (void)eatWithFoods:(NSDictionary *)foods;
- (void)runWithStep:(NSInteger)step;
- (void)run2WithStep:(NSInteger)step;

+ (void)decInfo:(NSString *)name age:(NSInteger)age;
+ (void)decInfo2:(NSString *)name age:(NSInteger)age;

@end

@implementation Teacher

- (void)eatWithFoods:(NSDictionary *)foods
{
    NSLog(@"========%@ eat foods: %@", _name, foods);
}

- (void)runWithStep:(NSInteger)step
{
    NSLog(@"========%s 1 %@ runs %ld steps", __func__, _name, step);
}

- (void)run2WithStep:(NSInteger)step
{
    NSLog(@"========%s 2 %@ runs %ld steps", __func__, _name, step);
}

+ (void)decInfo:(NSString *)name age:(NSInteger)age
{
    NSLog(@"========decInfo name: %@, age: %ld", name, age);
}

+ (void)decInfo2:(NSString *)name age:(NSInteger)age
{
    NSLog(@"========decInfo2 name: %@, age: %ld", name, age);
}

Adds a method

void rt_eatWithFoods2(id self, SEL _cmd, NSDictionary *foods)
{
    NSLog(@"========%@, %@ eat foods: %@", self, NSStringFromSelector(_cmd), foods);
}

- (void)viewDidLoad 
{
    [super viewDidLoad];
    SEL eatSel = NSSelectorFromString(@"rt_eatWithFoods:");
    [DYFRuntimeProvider addMethodWithClass:Teacher.class selector:eatSel impSelector:@selector(eatWithFoods:)];
    
    SEL eatSel2 = NSSelectorFromString(@"eatWithFoods2:");
    [DYFRuntimeProvider addMethodWithClass:Teacher.class selector:eatSel2 imp:(IMP)rt_eatWithFoods2 types:"v@:@"];
    
    Teacher *teacher = [[Teacher alloc] init];
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    if ([teacher respondsToSelector:eatSel]) {
        [teacher performSelector:eatSel withObject:@{@"name": @"meat", @"number": @1}];
    }
    
    if ([teacher respondsToSelector:eatSel2]) {
        [teacher performSelector:eatSel2 withObject:@{@"name": @"meat", @"number": @1}];
    }
    #pragma clang diagnostic pop
}

Exchanges two methods

- (void)viewDidLoad 
{
    [super viewDidLoad];
    [DYFRuntimeProvider exchangeMethodWithClass:Teacher.class selector:@selector(runWithStep:) anotherSelector:@selector(run2WithStep:)];
    Teacher *teacher = [[Teacher alloc] init];
    [teacher runWithStep:50];
    [teacher run2WithStep:100];
    
    [DYFRuntimeProvider exchangeClassMethodWithClass:Teacher.class selector:@selector(decInfo:age:) anotherSelector:@selector(decInfo2:age:)];
    [Teacher decInfo:@"David" age:40];
    [Teacher decInfo2:@"Liming" age:28];
}

Replaces a method

- (void)viewDidLoad 
{
    [super viewDidLoad];
    [DYFRuntimeProvider replaceMethodWithClass:Teacher.class selector:@selector(runWithStep:) targetSelector:@selector(run2WithStep:)];
    Teacher *teacher = [[Teacher alloc] init];
    [teacher runWithStep:50];
    [teacher run2WithStep:100];
}

Swizzle two methods

- (void)viewDidLoad 
{
    [super viewDidLoad];
    [DYFRuntimeProvider swizzleMethodWithClass:Teacher.class selector:@selector(runWithStep:) swizzledSelector:@selector(run2WithStep:)];
    Teacher *teacher = [[Teacher alloc] init];
    [teacher runWithStep:50];
    [teacher run2WithStep:100];
    
    [DYFRuntimeProvider swizzleClassMethodWithClass:Teacher.class selector:@selector(decInfo:age:) swizzledSelector:@selector(decInfo2:age:)];
    [Teacher decInfo:@"David" age:40];
    [Teacher decInfo2:@"Liming" age:28];
}

The transformation of dictionary and model

1. Converts the dictionary to model

Teacher *teacher = (Teacher *)[DYFRuntimeProvider asObjectWithDictionary:@{@"name": @"高粟", @"age": @26, @"address": @"xx市xx"} forClass:Teacher.class];
if (teacher) {
    NSLog(@"========teacher: %@, %@, %ld, %@", teacher, teacher.name, (long)teacher.age, teacher.address);
}

Teacher *teacher2 = [[Teacher alloc] init];
[DYFRuntimeProvider asObjectWithDictionary:@{@"name": @"高粟", @"age": @26, @"address": @"xx市xx"} forObject:teacher2];
NSLog(@"========teacher2: %@, %@, %ld, %@", teacher2, teacher2.name, (long)teacher2.age, teacher2.address);

2. Converts the model to dictionary

NSDictionary *dict = [DYFRuntimeProvider asDictionaryWithObject:teacher];
NSLog(@"========dict: %@", dict);

Archives and unarchives

Take this class as an example. e.g.:

@interface Transaction : NSObject <NSCoding>
@property (nonatomic, assign) NSUInteger state;
@property (nonatomic, copy) NSString *productIdentifier;
@property (nonatomic, copy) NSString *userIdentifier;
@property (nonatomic, copy) NSString *originalTransactionTimestamp;
@end

@implementation Transaction

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        [DYFRuntimeProvider decode:aDecoder forObject:self];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
 {
    [DYFRuntimeProvider encode:aCoder forObject:self];
}

@end

1. Archives

- (void)viewDidLoad 
{
    [super viewDidLoad];
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];
    NSString *filePath = [documentPath stringByAppendingPathComponent:@"Transaction.data"];
    [self archive:filePath];
}

- (void)archive:(NSString *)path 
{
    Transaction *transaction = [[Transaction alloc] init];
    [DYFRuntimeProvider archiveWithObject:transaction forClass:Transaction.class toFile:path];
}

Or

@implementation Transaction

- (void)encodeWithCoder:(NSCoder *)aCoder 
{
    [DYFRuntimeProvider encode:aCoder forObject:self];
}

@end

2. Unarchives

- (void)viewDidLoad 
{
    [super viewDidLoad];
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];
    NSString *filePath = [documentPath stringByAppendingPathComponent:@"Transaction.data"];
    [self unarchive:filePath];
}

- (void)unarchive:(NSString *)path 
{
    Transaction *transaction = [DYFRuntimeProvider unarchiveWithFile:path forClass:Transaction.class];
}

Or

@implementation Transaction

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        [DYFRuntimeProvider decode:aDecoder forObject:self];
    }
    return self;
}

@end

Add a catogory property

@interface UIApplication (Pt)
@property (nonatomic, strong) Teacher *teacher;
@end

static NSString *kTeacherKey = @"TeacherKey";

@implementation UIApplication (Pt)

- (Teacher *)teacher
{
    return (Teacher *)[DYFRuntimeProvider getAssociatedObject:self key:&kTeacherKey];
}

- (void)setTeacher:(Teacher *)teacher
{
    [DYFRuntimeProvider setAssociatedObject:self key:&kTeacherKey value:teacher policy:OBJC_ASSOCIATION_RETAIN_NONATOMIC];
}

@end
- (void)viewDidLoad 
{
    [super viewDidLoad];
    Teacher *teacher2 = [[Teacher alloc] init];
    [DYFRuntimeProvider asObjectWithDictionary:@{@"name": @"高粟", @"age": @26, @"address": @"xx市xx"} forObject:teacher2];
    UIApplication.sharedApplication.teacher = teacher2;
    NSLog(@"========teacher: %@", UIApplication.sharedApplication.teacher);
}

Get and modify instance variable property.

Teacher *teacher = (Teacher *)[DYFRuntimeProvider asObjectWithDictionary:@{@"name": @"高粟", @"age": @26, @"address": @"xx市xx"} forClass:Teacher.class];
NSString *teacherName = [DYFRuntimeProvider getInstanceVarWithName:@"_name" forObject:teacher];
NSLog(@"========teacher name: %@", teacherName);
[DYFRuntimeProvider setInstanceVarWithName:@"_name" value:@"李想" forObject:teacher];
NSLog(@"========teacher newName: %@", teacher.name);

Demo

DYFRuntimeProvider is learned how to use under this Demo.

Feedback is welcome

If you notice any issue, got stuck to create an issue. I will be happy to help you.

dyfruntimeprovider's People

Contributors

itenfay avatar

Stargazers

 avatar

Watchers

 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.