Question

I'm using the below code in my app :

@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

In this code 1 example, No need to use @property @synthesize and it work fine.

For the best practice, should I have to use 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

What is the best method, Code 1 or Code 2 or Code 3?

Was it helpful?

Solution

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

OTHER TIPS

I was always taught as part of "Proper" OO Design that member variables of an object should NOT be accessed directly, you should instead define methods (or properties) to read or manipulate those variables.

Objective-C takes some of the effort (fun?) out if this. You use @property and @synthesize so that you don't have to explicitly declare your properties and wory about memory management. This answer explains this behaviour in more in detail.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top