我很想知道,我试图用
访问一个对象id objc_getAssociatedObject(id object, void *key)
但是从
调用方法时它无效-(void)encodeWithCoderOv:(NSCoder *)aCoder
它返回null。
请帮忙!!! 这是代码。
+ (void)load {
MethodSwizzle(self, @selector(encodeWithCoder:),@selector(encodeWithCoderOv:));
MethodSwizzle(self, @selector(initWithCoder:), @selector(initWithCoderOv:));
}
// this method will return associated Object when its being called from anywhere,but will
// return null when its being called from encodeWithCoder.
- (id)getObject {
return [self associatedObject];
}
- (id)associatedObject {
return [objc_getAssociatedObject(self,&ObjectKey)retain];
}
- (void)setAssociatedObject:(UIImage*)newObject {
objc_setAssociatedObject(self,&ObjectKey,newObject,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(void)encodeWithCoderOv:(NSCoder *)aCoder {
[aCoder encodeObject:[self associatedObject] forKey:@"someKey"];
NSLog(@"object in encoder %@",[self associatedObject]); // object is null
}
答案 0 :(得分:0)
传递ObjectKey,而不是& ObjectKey。