xcode 覆盖@synthesize 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6492894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Override @synthesize method?
提问by Andrew
I have one class called dog and another class called cat.
我有一个班级叫狗和另一个班级叫猫。
Dog has an variable, "name" declared with @property in the .h file.
Dog 在 .h 文件中有一个用 @property 声明的变量“name”。
In the cat class, I have set the name by using the command, myDog.name = "buster", after creating the variable "myDog" of type dog.
在 cat 类中,在创建了 dog 类型的变量“myDog”之后,我使用命令 myDog.name = "buster" 设置了名称。
I would like to do additional operations when the name is set by overriding the set method normally created by the @synthesize command.
通过覆盖通常由@synthesize 命令创建的 set 方法来设置名称时,我想执行其他操作。
How can I do that? Thanks for the help!
我怎样才能做到这一点?谢谢您的帮助!
回答by Chance Hudson
All you have to do is leave the @synthesize then create whichever methods you want to be custom. Example:
您所要做的就是离开@synthesize,然后创建您想要自定义的任何方法。例子:
In .h
在.h
@property(nonatomic, retain)NSString *Bob;
In .m
在.m
@synthesize bob;
-(void)setBob:(NSString *)bobValue{
[bobValue retain];
[bob release];
bob = bobValue;
//your custom stuffs here
}
回答by Perception
This has been pretty much answered on SO already - see Objective-C synthesize property name overridingfor details. In particular, @Dev Kanchen's answer which includes example code.
这已经在 SO 上得到了很多回答 -有关详细信息,请参阅Objective-C 合成属性名称覆盖。特别是@Dev Kanchen 的答案,其中包括示例代码。
回答by Regexident
You cannot override (and call it within)a synthesized method from within the very same class.
您不能从同一个类中覆盖(并在其中调用)合成方法。
You can however override it from a subclass(or rather: synthesize it in an abstract superclass).
但是,您可以从子类覆盖它(或者更确切地说:在抽象超类中合成它)。
If you simply want to perform additional (vs. different) operations upon property change I would use KVOby simply adding each dog as observer to its own "name"
property in -(id)init;
.
如果你只是想执行额外的(对不同)在属性更改操作,我会用国际志愿者组织通过简单地增加每只狗作为观察员自己的"name"
财产-(id)init;
。
Edit:
编辑:
There is a way to add additional logic to synthesized methods from within the same class:
Define a private intermediate property in a class extension.
有一种方法可以从同一类中向合成方法添加额外的逻辑:
在类扩展中定义私有中间属性。
I've attached source code for a class which uses synthesized properties and takes care(sic!) of keeping the dog's owner in sync with its own identity.
我附上了一个类的源代码,该类使用合成属性并注意(原文如此!)保持狗的主人与其自己的身份同步。
Dog.h:
狗.h:
#import <Foundation/Foundation.h>
@interface Dog : NSObject {
@private
NSString *name;
NSString *owner;
}
@property (nonatomic, readwrite, retain) NSString *name;
@property (nonatomic, readwrite, retain) NSString *owner;
@end
Dog.m:
狗.m:
#import "Dog.h"
@interface Dog ()
@property (nonatomic, readwrite, retain) NSString *primitiveName;
@end
@implementation Dog
@dynamic name;
@synthesize primitiveName = name;
@synthesize owner;
- (id)init {
if ((self = [super init])) {
name = @"Snowy";
owner = @"Tintin";
}
return self;
}
- (void)dealloc {
[super dealloc];
}
- (NSString *)name {
return self.primitiveName;
}
- (void)setName:(NSString *)aName {
self.primitiveName = aName;
if ([aName isEqualToString:@"Snoopy"]) {
self.owner = @"Charlie Brown";
}
else if ([aName isEqualToString:@"Snowy"]) {
self.owner = @"Tintin";
}
}
- (NSString *)description {
return [NSString stringWithFormat:@"<%@ name:'%@' owner:'%@'>", [self class], self.name, self.owner];
}
@end
Test:
测试:
Dog *dog = [[Dog alloc] init];
NSLog(@"%@", dog);
dog.name = @"Snoopy";
NSLog(@"%@", dog);
dog.name = @"Snowy";
NSLog(@"%@", dog);
Result:
结果:
<Dog name:'Snowy' owner:'Tintin'>
<Dog name:'Snoopy' owner:'Charlie Brown'>
<Dog name:'Snowy' owner:'Tintin'>