Objective-C 2.0 中增加了@dynamic 指令,表示变量对应的属性访问器方法,是动态实现的,你需要在NSObject 中继承而来的+(BOOL) resolveInstanceMethod:(SEL) sel 方法中指定动态实现的方法或者函数。
Person.m:
Person.h:
@interface Person : NSObject{
NSString *name;
float weight;
}
-(Person*) initWithWeight: (int) weight;
@property (retain,readwrite) NSString* name;
@property (readonly)float weight;
@property float height;
-(void) print: (NSString*) str;
@end
Person.m:
void dynamicMethod(id self,SEL _cmd,float w){
printf("dynamicMethod-%s\n",[NSStringFromSelector(_cmd)
cStringUsingEncoding:NSUTF8StringEncoding]);
printf("%f\n",w);
}
@implementation Person
@synthesize name;
@synthesize weight;
@dynamic height;
-(Person*) initWithWeight: (int) w{
self=[super init];
if (self) {
weight=w;
}
return self;
}
-(void) print: (NSString*) str{
NSLog(@"%