AYRuntime 1.0.5

AYRuntime 1.0.5

测试已测试
Lang语言 Obj-CObjective C
许可 MIT
发布最后发布2017年9月

Alan Yeh维护。



AYRuntime 1.0.5

  • Alan Yeh

引用

  您可以使用CocoaPods方便地引入AYRuntime。在 Podfile 中添加对 AYRuntime 的依赖。

pod "AYRuntime"

简介

  AYRuntime 包含了一些关于 Runtime 的类库。

AYBlockInvocation

  AYBlockInvocation 可以很方便地动态调用 block。用法与 NSInvocation 非常相似。

    CGPoint (^block)(CGPoint) = ^CGPoint(CGPoint point) {
        XCTAssert(CGPointEqualToPoint(CGPointMake(1.0, 1.0), point));
        return CGPointMake(point.x + 1.0f, point.y + 1.0f);
    };

    AYBlockInvocation *invocation = [AYBlockInvocation invocationWithBlock:block];
    CGPoint point = CGPointMake(1.0, 1.0);
    //NSInvocation的参数从2开始,而AYBlockInvocation的参数从1开始
    [invocation setArgument:&point atIndex:1];

    [invocation invoke];
    CGPoint result;
    [invocation getReturnValue:&result];
    XCTAssert(CGPointEqualToPoint(CGPointMake(2.0, 2.0), result));

AYDeallocNotifier

  AYDeallocNotifier 利用 runtime,在对象销毁时,可以执行一些动作。

    id ex = [self expectationWithDescription:@""];

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        AYTestSwizzleClass *object = [AYTestSwizzleClass new];
        [object ay_notifyWhenDealloc:^{
            [ex fulfill];
        }];
    });

    [self waitForExpectationsWithTimeout:1 handler:nil];

runtime.h

#define objc_AssociationKey(key) static const void * const key = &key
#define objc_AssociationKeyAndNotes(key, notes) static const void * const key = &key

FOUNDATION_EXPORT void class_swizzleSelector(Class class, SEL originalSelector, SEL newSelector);

FOUNDATION_EXPORT void class_enumerateMethodList(Class class, void(^enumerator)(Class class, Method method));

FOUNDATION_EXPORT IMP class_replaceClassMethodWithBlock(Class cls, SEL originalSelector, id block);

FOUNDATION_EXPORT IMP class_replaceInstanceMethodWithBlock(Class cls, SEL originalSelector, id block);

FOUNDATION_EXPORT Method class_getClassMethod(Class cls, SEL selector);

FOUNDATION_EXPORT Method class_getInstanceMethod(Class cls, SEL selector);

FOUNDATION_EXPORT id objc_getAssociatedDefaultObject(id object, const void *key, id defaultObject, objc_AssociationPolicy policy);

FOUNDATION_EXPORT id objc_getAssociatedDefaultObjectBlock(id object, const void *key, objc_AssociationPolicy policy, id (^defaultObject)());

许可

AYRuntime 在 MIT 许可下可用。更多信息请查阅 LICENSE 文件。