是否可以生成Obj-C方法? - 例如:
- (void)doSomething:(BOOL)isLater
{
if (isLater == FALSE)
{
NSLog(@"Not Later");
} else {
NSLog(@"Is Later");
}
}
我特别谈论生成逻辑,我有一些代码可以创建一个类并添加一个现有的方法,但我想知道如果我可以为方法本身生成逻辑并将其添加到方法
static NSString *Description(id self, SEL _cmd)
{
return @"My Description Method";
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
Class mySubclass = objc_allocateClassPair([NSObject class], "MySubclass", 0);
// grab NSObject's description signature so we can borrow it
Method description = class_getInstanceMethod([NSObject class],
@selector(description));
const char *types = method_getTypeEncoding(description);
// now add
class_addMethod(mySubclass, @selector(description), (IMP)Description, types);
objc_registerClassPair(mySubclass);
id myInstance = [[mySubclass alloc] init];
NSLog(@"%@", myInstance);
}