Alright, here's my code for a simple PickerView and it's working too.
@interface ViewController ()
@property NSArray *moods;
@end
@implementation ViewController
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [[self moods] count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return self.moods[row];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.moods = @[@"I" , @"Love", @"Stack", @"Overflow", @"Great!"];
}
My question is about the confusing behaviour about the setter and getter method using properties. To further clarify, I don't know why the .(dot) notation of setter method works and the bracket style doesn't.
For example, under this method
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return self.moods[row];
}
If I type it in as
return [self moods[row]];
That doesn't work. Why? It says missing identifier and on further adding it too like this
return [self moods[[row]]];
It doesn't simply work at all. It keeps on asking to add the identifier error.
Same goes for this method, where the following works fine
self.moods = @[@"I" , @"Love", @"Stack", @"Overflow", @"Great!"];
BUT the following code style doesn't work too
[self moods] = @[@"I" , @"Love", @"Stack", @"Overflow", @"Great!"];
or even the setValue forKey ones in my case.
Alright, I agree that I'm fairly new to this and trying to self learn. I tried learning Objective-C and doing my best at it last week and covered all those concepts there. But it's my second day in iOS programming and am finding this minor stuff difficult to grasp.
Can you please help and guide a bit in detail? While following a bit of Stanford University classes video on iOS, they recommend to always use dot notation for properties. But you can use both, right? So I need to understand and build solid foundation. Thanks!
id x = self.moods
=> getter and its equal to id x = [self moods]
self.moods = array
=> setter and its equal to [self setMoods:newArray]
to access items:
self.moods[index]
OR [self moods][index]