Anonymous classes can be implemented with library. Several months ago I have worked on MMMutableMethods
fork to improve old implementation (discussing with author) and to add my own mechanism without any obj-c runtime manipulation.
https://github.com/k06a/MMMutableMethods
A. First mechanism works on obj-c runtime class creation:
MM_CREATE(MM_REUSE,^(Class class){
[class addMethod:@selector(onResultWithId:)
fromProtocol:@protocol(AMCommandCallback)
blockImp:^(id this,id res){
NSLog(@"onResultWithId: %@",res);
}];
[class addMethod:@selector(onErrorWithJavaLangException:)
fromProtocol:@protocol(AMCommandCallback)
blockImp:^(id this,JavaLangException *e){
NSLog(@"onErrorWithJavaLangException: %@",e);
}];
})
B. Second mechanism works on simple message forward implementation:
MM_ANON(^(MMAnonymousClass *anon){
[anon addMethod:@selector(onResultWithId:)
fromProtocol:@protocol(AMCommandCallback)
blockImp:^(id this,id res){
NSLog(@"onResultWithId: %@",res);
}];
[anon addMethod:@selector(onErrorWithJavaLangException:)
fromProtocol:@protocol(AMCommandCallback)
blockImp:^(id this,JavaLangException *e){
NSLog(@"onErrorWithJavaLangException: %@",e);
}];
})
First one creates new obc-j classes in runtime, it allows you to create classes MM_CREATE_CLASS(MM_REUSE, *)
and directly instances with MM_CREATE(MM_REUSE, *)
. Classes will be created only on first execution and reused by default, but you can avoid reusing by calling MM_CREATE_CLASS_ALWAYS(*)
and MM_CREATE_ALWAYS(*)
.
The second mechanism doesn't creates any runtime instances, just remember blocks for selectors and forward method calls to them.
I prefere second way not to create a lot of classes in runtime. IMHO it is much safer and enough powerful.
To use this library just:
pod 'MMMutableMethods', :git => 'https://github.com/k06a/MMMutableMethods'