ios @property 的自定义设置器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6843125/
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
Custom setter for @property?
提问by Alex
How can i use a custom setter for the following property after I synthesized it ?
合成后如何为以下属性使用自定义设置器?
@property (nonatomic,retain) UIButton *but
回答by deanWombourne
@Sascha is almost right but his code has a tiny bug in it ;)
@Sascha 几乎是对的,但他的代码中有一个小错误;)
It would look like either :
它看起来像:
A)
一种)
-(void)setBut:(UIButton *)value {
if (but != value) {
[but release];
but = [value retain];
}
}
or B)
或 B)
-(void)setBut:(UIButton *)value {
[but autorelease];
but = [value retain];
}
(A) is (very) slightly more efficient, (B) is more readable.
(A)(非常)稍微更有效,(B)更具可读性。
Why do we need the if statement in option (A) instead of just the release & retain in @Sascha's answer?
为什么我们需要选项 (A) 中的 if 语句,而不仅仅是@Sascha 的回答中的 release & retain?
What happens if you pass in the same object twice?
如果两次传入同一个对象会发生什么?
i.e.
IE
// We set our button for the first time
UIButton *test = [UIButton alloc] init];
[self setBut:test];
[test release];
// Much later in the code, we set the button again
[self setBut:test];
If we didn't check that but
wasn't a different object, the first thing we would do in our setter is release
it. We would then try to retain
an object that doesn't exist anymore, causing a crash.
如果我们没有检查这but
不是一个不同的对象,我们在 setter 中要做的第一件事就是release
它。然后我们会尝试retain
一个不再存在的对象,导致崩溃。
NB We don't need the if statement in option (B) because autorelease won't immediately release the button so we have time to retain it again without it being dealloc'd.
注意我们不需要选项 (B) 中的 if 语句,因为 autorelease 不会立即释放按钮,因此我们有时间再次保留它而不会被释放。
回答by andreamazz
@property (getter=yourGetter,setter=yourSetter:) UIButton *but;
回答by C?ur
A B+) alternative to deanWombourne solutions:
A B+) deanWombourne 解决方案的替代方案:
-(void)setBut:(UIButton *)value {
[value retain]
[but release];
but = value;
}
This solution will prevent issues where valueis a sub-object of but.
此解决方案将防止value是but的子对象的问题。
An A+) alternative to deanWombourne solutions:
deanWombourne 解决方案的 A+) 替代方案:
-(void)setBut:(UIButton *)value {
if (but != value) {
[value retain]
//insert here but's cancel, invalidate, delegate = nil, ...
[but release];
but = value;
}
}
This solution will prevent issues where valueis a sub-object of but.
And it will allow you to add a cancel
for an NSURLConnection, an invalidate
an NSTimer or NSPort, a nil for a delegate, ...
此解决方案将防止value是but的子对象的问题。它将允许您cancel
为 NSURLConnection添加一个,invalidate
一个 NSTimer 或 NSPort,一个为委托添加一个 nil,...
回答by Simon Tillson
I believe this is how the @synthesised setters do it, and it works in all situations, regardless of whether you assign the same object or not:
我相信这就是@synthesised setter 的工作方式,它适用于所有情况,无论您是否分配相同的对象:
- (void)setBut: (UIButton*)aButton
{
id oldObject = but;
but = [aButton retain];
[oldObject release];
}
Cannot go wrong, as far as I can see.
就我所见,不会出错。
回答by Sascha
Implement
实施
- (void)setBut:(UIButton *)aButton;
It should probably look something like
它应该看起来像
- (void)setBut:(UIButton *)aButton {
[but release];
but = [aButton retain];
// whatever
}