0

私は私のアプリで以下のコードを使用しています:

@interface OMNController : NSObject
{
  IBOutlet NSSearchField *filterFieldMonitor;
  OMNMonitorTableView *monitorTableView;
}


@implementation OMNController
- (id) init
{
  monitorTableView = [[OMNMonitorTableView alloc] init];
  NSString *l_filter = [filterFieldMonitor stringValue];
  [monitorTableView setFilter:l_filter];
}
  ....
@end

このコード1の例では、@ property @synthesizeを使用する必要はなく、正常に機能します。

ベストプラクティスとして、accessor / ivarを使用する必要があります:

@interface OMNController : NSObject
{
  IBOutlet NSSearchField *_filterFieldMonitor;
  OMNMonitorTableView *_monitorTableView;
}
@property (readwrite, retain) OMNMonitorTableView *monitorTableView;
@property (assign) IBOutlet NSSearchField *filterFieldMonitor;;


@implementation OMNController

@synthesize monitorTableView = _monitorTableView;
@synthesize filterFieldMonitor = _filterFieldMonitor;

- (id) init
{
  self.monitorTableView = [[OMNMonitorTableView alloc] init];
  NSString *l_filter = [self.filterFieldMonitor stringValue];
  [self.monitorTableView setFilter:l_filter];
}
  ....
@end

-

@interface OMNController : NSObject
{
  IBOutlet NSSearchField *filterFieldMonitor;
  OMNMonitorTableView *monitorTableView;
}
@property (readwrite, retain) OMNMonitorTableView *monitorTableView;
@property (assign) IBOutlet NSSearchField *filterFieldMonitor;;


@implementation OMNController

@synthesize monitorTableView;
@synthesize filterFieldMonitor;

- (id) init
{
  monitorTableView = [[OMNMonitorTableView alloc] init];
  NSString *l_filter = [filterFieldMonitor stringValue];
  [monitorTableView setFilter:l_filter];
}
  ....
@end

コード1またはコード2またはコード3の最良の方法は何ですか?

4

2 に答える 2

1

First of all, there's considerable debate in the Cocoa community regarding whether one should call accessors in init/dealloc or not. See related questions here, here, and here. Personally, I fall into the "don't do that" camp, but again, it's debatable.

Second, with the modern runtime you don't need to declare ivars at all. Just declare your properties, and be done with it. The ivars are synthesized automatically.

Third, for properties only used internally (not outside the class in which they're defined), there's really no reason to put them in the header file at all. You can declare them in the implementation, as a class extension.

Finally, for objects that are likely to be created only once, I've taken to creating them lazily in the accessor, rather than explicitly in init.

Given all that, here's how I'd likely write it:

// OMNController.h
@interface OMNController : NSObject
@end

// OMNController.m
@interface OMNController ()
@property (nonatomic, retain) OMNMonitorTableView *monitorTableView;
@property (nonatomic, retain) IBOutlet NSSearchField *filterFieldMonitor;
@end

@implementation OMNController

@synthesize monitorTableView = _monitorTableView;
@synthesize filterFieldMonitor = _filterFieldMonitor;

- (OMNMonitorTableView*) monitorTableView 
{
    if( !_monitorTableView ) {
        _monitorTableView = [[OMNMonitorTableView alloc] init];
        NSString *l_filter = [self.filterFieldMonitor stringValue];
        [_monitorTableView setFilter:l_filter];
    }
    return _monitorTableView;
}

@end
于 2012-06-12T09:39:05.310 に答える
0

私は常に「適切な」オブジェクト指向デザインの一部として、オブジェクトのメンバー変数に直接アクセスしてはならず、代わりにそれらの変数を読み取ったり操作したりするメソッド(またはプロパティ)を定義する必要があると教えられていました。

Objective-Cは、これがあれば、いくらかの努力(楽しい?)を取り除きます。@propertyを使用する@synthesizeので、プロパティを明示的に宣言したり、メモリ管理について心配したりする必要はありません。この回答は、この動作をより詳細に説明しています。

I'd also suggest taking a look at Declared Properties on the Apple Developer Library

于 2012-06-12T06:26:16.807 に答える