Joywii / Swizzle

Objective-C Method Swizzling Architecture And Examples(NSArray and NSMutableArray)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Swizzle

####描述 实现了一个Objective-C语言Method Swizzling的框架,并使用该框架针对NSArray和NSMutableArray中的objectAtIndex:进行了下标安全检查的替换。 ####使用

  • 导入NSObject+Swizzle.hNSObject+Swizzle.m到你的工程。
  • 添加想要Method Swizzling方法的类的Category,例如:
@interface NSArray (Swizzle)
@end
@implementation NSArray (Swizzle)
+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^
    {
        @autoreleasepool
        {
            NSError *swizzleError;
            [objc_getClass("__NSArrayI") swizzleMethod:@selector(objectAtIndex:) withMethod:@selector(safe_objectAtIndex:) error:&swizzleError];
            if (swizzleError)
            {
                NSLog(@"%@",swizzleError);
            }
        };
    });
}
- (id)safe_objectAtIndex:(NSUInteger)index
{
    if (index >= [self count])
    {
        return nil;
    }
    return [self safe_objectAtIndex:index];
}
@end

####参考

####License Swizzle is distributed under the terms and conditions of the MIT license.

About

Objective-C Method Swizzling Architecture And Examples(NSArray and NSMutableArray)

License:MIT License


Languages

Language:Objective-C 100.0%