English 中文(简体)
ARC 和坚固的设置
原标题:ARC and strong setters

考虑:

@interface MyView : UIView
@property (strong, nonatomic) UILabel * label ;
@end

并且:

@implementation MyView
@synthesize label ;
- (void) setLabel: (UILabel *) label_ {
    self->label = label_ ;
    // ... custom stuff for when the label is changed from the outside
}
@end

最理想的情况是,我想把默认的合成设置器设为陷阱,这样它就能在 ARC 下做正确的事情, 并且只添加我需要的东西, 以便用这个新标签更新我的上下文 。

我担心这条线:

self->label = label_ ;

只要绕过任何 默认合成安装器 本来会保证 如果我没有选择 提供我自己的。

推翻 ARC 下的默认合成设置器的正确方式是什么?

最佳回答

在 ARC 下默认设置器仅为 :

label = label_;

如果你再用下下划线的话 你也许用上划线的名字 作为支持的伊瓦尔,所以:

@synthesize label = label_;

并且:

- (void) setLabel: (UILabel *) label{
    label_ = label;
    // ... custom stuff for when the label is changed from the outside
}

这是一个非常典型的 被压倒的进入器。

问题回答

我提出一个替代方案。 您设置自己作为观察者, 以修改标签属性, 在您传递为选择器的方法中做额外的东西。 它更是模块化, 更不可能破坏 。





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签