Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing accessor methods to set iVars?
    text
    copied!<p>Initially, I was looking at the way "pickerData" is set and thinking I wonder why you can't just assign it directly (as in METHOD_002), but then I stated thinking that I should really be using the accessor methods I defined and not setting the instance variables directly. Am I understand this correctly that METHOD_001 is a better way of doing this?</p> <pre><code>@property(nonatomic, retain) IBOutlet NSArray *pickerData; </code></pre> <p>METHOD_001</p> <pre><code>-(void)viewDidLoad { NSLog(@"VIEW: Single ... Loaded"); NSArray *dataArray = [[NSArray alloc] initWithObjects:@"A", @"B", @"C",nil]; [self setPickerData:dataArray]; [dataArray release]; [super viewDidLoad]; } -(void)dealloc { [pickerData release]; [super dealloc]; } </code></pre> <p>OR METHOD_002</p> <pre><code>-(void)viewDidLoad { NSLog(@"VIEW: Single ... Loaded"); if(pickerData != nil) [pickerData release]; pickerData = [[[NSArray alloc] initWithObjects:@"A", @"B", @"C", nil] retain]; [super viewDidLoad]; } -(void)dealloc { [pickerData release]; [super dealloc]; } </code></pre> <h1>EDIT_001:</h1> <p>First off I have added the "nil" values to terminate the NSArrays, coming from C I always forget this, my bad. Also you're right, I did not account for the fact in METHOD_002 that pickerData might already be set and as a result leak the old object. Once you start noticing these problems and fixing the code it starts to look like METHOD_001 is the best idea. Or to just use the property directly as Vladimir and eJames noted.</p> <pre><code>self.pickerData = [NSArray arrayWithObjects: @"A", @"B", @"C", nil]; </code></pre> <h1>EDIT_002:</h1> <p>Thank you for all the pointers and comments, for now I am going to stick with METHOD_001, I could just as easily use NSArrayWithObjects: but I am trying to keep memory usage low by releasing things myself as soon as I can (not that it matters here, but for future projects) Also I do like the feel of self.pickerData, but I am still unsure how I feel about dot-notation and have for now been trying to stick with old style objects and messages where possible. Again many thanks for the help.</p> <p>gary</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload