All Projects → Tuccuay → Runtimesummary

Tuccuay / Runtimesummary

Licence: mit
一个集合了常用 Objective-C Runtime 使用方法的 Playground。

Projects that are alternatives of or similar to Runtimesummary

observable-playground
Know your Observables before deploying to production
Stars: ✭ 96 (-65.84%)
Mutual labels:  playground
Dynamic Struct
Golang package for editing struct's fields during runtime and mapping structs to other structs.
Stars: ✭ 257 (-8.54%)
Mutual labels:  runtime
Swiftpascalinterpreter
Simple Swift interpreter for the Pascal language inspired by the Let’s Build A Simple Interpreter article series.
Stars: ✭ 270 (-3.91%)
Mutual labels:  playground
taro-playground
The Taro Playground App is a cross-platform application developed using Taro, to help developers develop and debug Taro applications.
Stars: ✭ 33 (-88.26%)
Mutual labels:  playground
Urde
Data interchange and engine re-implementation for games by Retro Studios | Mirror
Stars: ✭ 253 (-9.96%)
Mutual labels:  runtime
Ios Interview Questions
iOS面试题整理,在线查看地址:https://ios.nobady.cn
Stars: ✭ 258 (-8.19%)
Mutual labels:  runtime
go-playground
GNU/Emacs mode that setup local Go playground for code snippets like play.golang.org or even better :)
Stars: ✭ 64 (-77.22%)
Mutual labels:  playground
Livecodelab
a web based livecoding environment
Stars: ✭ 276 (-1.78%)
Mutual labels:  playground
Ddetours
Delphi Detours Library
Stars: ✭ 256 (-8.9%)
Mutual labels:  runtime
Ember Twiddle
JSFiddle type thing for ember-cli style code
Stars: ✭ 269 (-4.27%)
Mutual labels:  playground
xcode-config
My Xcode config - Alfred Workflow, File templates, Xcode code snippets, themes, IDETextKeyBindingSet
Stars: ✭ 16 (-94.31%)
Mutual labels:  cocoa
ms-rest-js
Runtime for isomorphic javascript libraries generated by Autorest
Stars: ✭ 54 (-80.78%)
Mutual labels:  runtime
Charm4py
Parallel Programming with Python and Charm++
Stars: ✭ 259 (-7.83%)
Mutual labels:  runtime
AMLeaksFinder
A small tool for automatically detecting the [controller, view memory leak] in the project. 一款用于自动检测项目中【控制器内存泄漏,View 内存泄漏】的小工具,支持 ObjC,Swift。
Stars: ✭ 89 (-68.33%)
Mutual labels:  runtime
Dvm
Deno Version Manager - Easy way to manage multiple active deno versions.
Stars: ✭ 271 (-3.56%)
Mutual labels:  runtime
Eloquent
Eloquent is a bible study tool for macOS
Stars: ✭ 88 (-68.68%)
Mutual labels:  cocoa
Playgroundbook
Tool for Swift Playground books
Stars: ✭ 257 (-8.54%)
Mutual labels:  playground
Nshipster.com
A journal of the overlooked bits in Objective-C, Swift, and Cocoa. Updated weekly.
Stars: ✭ 280 (-0.36%)
Mutual labels:  cocoa
Xcode One Dark
Atom One Dark theme for Xcode
Stars: ✭ 273 (-2.85%)
Mutual labels:  cocoa
Swift Diagram Playgrounds
Drawing diagrams in Swift using a recursive enum data structure
Stars: ✭ 264 (-6.05%)
Mutual labels:  playground

RuntimeSummary

RuntimeSummary

一个集合了常用 Objective-C Runtime 使用方法的 Playground

目录

如何使用这个 Playground

Screenshot

先选择 Scheme,然后 Run!

消息机制介绍 / Messaging

// 创建一个对象
// Cat *harlan = [[Cat alloc] init];

// 使用 Runtime 创建一个对象
// 根据类名获取到类
Class catClass = objc_getClass("Cat");

// 同过类创建实例对象
// 如果这里报错,请将 Build Setting -> Enable Strict Checking of objc_msgSend Calls 改为 NO
Cat *harlan = objc_msgSend(catClass, @selector(alloc));

// 初始化对象
// harlan = [harlan init];

// 通过 Runtime 初始化对象
harlan = objc_msgSend(harlan, @selector(init));

// 调用对象方法
// [harlan eat];

// 通过 Runtime 调用对象方法
// 调用的这个方法没有声明只有实现所以这里会有警告
// 但是发送消息的时候会从方法列表里寻找方法
// 所以这个能够成功执行
objc_msgSend(harlan, @selector(eat));

// 输出: 2016-04-21 21:10:20.733 Messaging[20696:1825249] burbur~

// 当然,objc_msgSend 可以传递参数
Cat *alex = objc_msgSend(objc_msgSend(objc_getClass("Cat"), sel_registerName("alloc")), sel_registerName("init"));
objc_msgSend(alex, @selector(run:), 10);

方法交换 / MethodSwizzling

+ (void)load {
    // 获取到两个方法
    Method imageNamedMethod = class_getClassMethod(self, @selector(imageNamed:));
    Method tuc_imageNamedMethod = class_getClassMethod(self, @selector(tuc_imageNamed:));

    // 交换方法
    method_exchangeImplementations(imageNamedMethod, tuc_imageNamedMethod);
}

+ (UIImage *)tuc_imageNamed:(NSString *)name {
    // 因为来到这里的时候方法实际上已经被交换过了
    // 这里要调用 imageNamed: 就需要调换被交换过的 tuc_imageNamed
    UIImage *image = [UIImage tuc_imageNamed:name];

    // 判断是否存在图片
    if (image) {
        NSLog(@"加载成功");
    } else {
        NSLog(@"加载失败");
    }

    return image;
}

动态加载方法 / ResolveInstanceMethod

void run(id self, SEL _cmd,  NSNumber *metre) {
    NSLog(@"跑了%@米",metre);
}

// 当调用了一个未实现的方法会来到这里
+ (BOOL)resolveInstanceMethod:(SEL)sel {
    if (sel == NSSelectorFromString(@"run:")) {
        // 动态添加 run: 方法
        class_addMethod(self, @selector(run:), run, "[email protected]:@");

        return YES;
    }

    return [super resolveInstanceMethod:sel];
}

消息转发 / ForwardMessage

#pragma mark - 实例方法

//  第一步
//  在没有找到方法时,会先调用此方法,可用于动态添加方法
//  返回 YES 表示相应 selector 的实现已经被找到并添加到了类中,否则返回 NO

+ (BOOL)resolveInstanceMethod:(SEL)sel {
    return YES;
}

//  第二步
//  如果第一步的返回 NO 或者直接返回了 YES 而没有添加方法,该方法被调用
//  在这个方法中,我们可以指定一个可以返回一个可以响应该方法的对象
//  如果返回 self 就会死循环

- (id)forwardingTargetForSelector:(SEL)aSelector {
    return nil;
}

//  第三步
//  如果 `forwardingTargetForSelector:` 返回了 nil,则该方法会被调用,系统会询问我们要一个合法的『类型编码(Type Encoding)』
//  https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html
//  若返回 nil,则不会进入下一步,而是无法处理消息

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
    return [NSMethodSignature signatureWithObjCTypes:"[email protected]:"];
}

// 当实现了此方法后,-doesNotRecognizeSelector: 将不会被调用
// 如果要测试找不到方法,可以注释掉这一个方法
// 在这里进行消息转发
- (void)forwardInvocation:(NSInvocation *)anInvocation {
    // 我们还可以改变方法选择器
    [anInvocation setSelector:@selector(touch)];
    // 改变方法选择器后,还需要指定是哪个对象的方法
    [anInvocation invokeWithTarget:self];
}

- (void)touch {
    NSLog(@"Cat 没有实现 -stoke 方法,并且成功的转成了 -touch 方法");
}

- (void)doesNotRecognizeSelector:(SEL)aSelector {
    NSLog(@"无法处理消息:%@", NSStringFromSelector(aSelector));
}

动态关联属性 / AssociatedObject

- (void)setName:(NSString *)name {
    // 把属性关联给对象
    objc_setAssociatedObject(self, "name", name, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)name {
    // 取出属性
    return objc_getAssociatedObject(self, "name");
}

字典转模型 / MakeModel

+ (instancetype)modelWithDict:(NSDictionary *)dict updateDict:(NSDictionary *)updateDict {
    id model = [[self alloc] init];
    // 遍历模型中属性
    unsigned int count = 0;
    Ivar *ivars = class_copyIvarList(self, &count);
    for (int i = 0 ; i < count; i++) {
        Ivar ivar = ivars[i];
        // 属性名称
        NSString *ivarName = [NSString stringWithUTF8String:ivar_getName(ivar)];
        ivarName = [ivarName substringFromIndex:1];
        id value = dict[ivarName];
        // 模型中属性名对应字典中的key
        if (value == nil) {
            if (updateDict) {
                NSString *keyName = updateDict[ivarName];
                value = dict[keyName];
            }
        }
        [model setValue:value forKeyPath:ivarName];
    }
    return model;
}

+ (instancetype)modelWithDict:(NSDictionary *)dict {
    return [self modelWithDict:dict updateDict:nil];
}

对象归档、解档 / ObjectArchive

- (void)tuc_initWithCoder:(NSCoder *)aDecoder {
    // 不光归档自身的属性,还要循环把所有父类的属性也找出来
    Class selfClass = self.class;
    while (selfClass &&selfClass != [NSObject class]) {

        unsigned int outCount = 0;
        Ivar *ivars = class_copyIvarList(selfClass, &outCount);
        for (int i = 0; i < outCount; i++) {
            Ivar ivar = ivars[i];
            NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];

            // 如果有实现忽略属性的方法
            if ([self respondsToSelector:@selector(ignoredProperty)]) {
                // 就跳过这个属性
                if ([[self ignoredProperty] containsObject:key]) continue;
            }

            id value = [aDecoder decodeObjectForKey:key];
            [self setValue:value forKey:key];
        }
        free(ivars);
        selfClass = [selfClass superclass];
    }

}

- (void)tuc_encodeWithCoder:(NSCoder *)aCoder {
    Class selfClass = self.class;
    while (selfClass &&selfClass != [NSObject class]) {

        unsigned int outCount = 0;
        Ivar *ivars = class_copyIvarList([self class], &outCount);
        for (int i = 0; i < outCount; i++) {
            Ivar ivar = ivars[i];
            NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];

            if ([self respondsToSelector:@selector(ignoredProperty)]) {
                if ([[self ignoredProperty] containsObject:key]) continue;
            }

            id value = [self valueForKeyPath:key];
            [aCoder encodeObject:value forKey:key];
        }
        free(ivars);
        selfClass = [selfClass superclass];
    }
}

更多

觉得还有其它常用的 Runtime 用法没有讲到?欢迎 Pull Request!

或者懒得动手想看看大家的思路?没问题,提个 issue

觉得看了这个 Playground 豁然开朗?来 Star 一个吧!

License

RuntimeSummary is released under the MIT license. See LICENSE .

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].