Swizzle
Objective-C Method Swizzling Architecture And Examples(NSArray and NSMutableArray)
Install / Use
/learn @Joywii/SwizzleREADME
Swizzle
####描述
实现了一个Objective-C语言Method Swizzling的框架,并使用该框架针对NSArray和NSMutableArray中的objectAtIndex:进行了下标安全检查的替换。
####使用
- 导入
NSObject+Swizzle.h和NSObject+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.
Related Skills
node-connect
341.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
84.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
341.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
84.6kCommit, push, and open a PR
