Hi all,
So I have been trying to wrap my head around the bit of code below - I *think* I understand what it's doing. The question is am I?
Here is the code:
As I understand this:
If the cards array doesn't exist - then create one. This way we only ever create the array when we need it - memory optimisation - right?
Next is this bit of code (Using the not operator again )
As I understand this:
If the UIButton is not selected - then tapping the button will make it UIControlState selected (UIControlStateSelected)
if the button is selected this will reverse the above line of code and change it's state to: (UIControlStateNormal) - right?
As far as I understand this operator - it does the opposite. So in the example of our cards araay:
If it does exist - do nothing. (Method won't run) if it doesn't (not) exist then it will create an array of cards..
My problem is just when I am reading these for of lines of code that include the not operator - sometimes it isn't 100% clear what it's doing. Is there an *easy* way to rethink of this operator for better understanding when writing / reading code?
P.S: Sorry for all the n00b questions of late - my first few months of being an actual iOS developer is providing a steep learning curve. Then again I am trying to get up to speed ASAP so maybe that's why I am stumbling a little?
Thanks again all!
So I have been trying to wrap my head around the bit of code below - I *think* I understand what it's doing. The question is am I?
Here is the code:
Code:
//Lazy instaintation
- (NSMutableArray *)cards
{
if (!_cards) _cards = [[NSMutableArray alloc]init];
return _cards;
}
As I understand this:
If the cards array doesn't exist - then create one. This way we only ever create the array when we need it - memory optimisation - right?
Next is this bit of code (Using the not operator again )
Code:
- (IBAction)newGameButton:(UIButton *)sender
{
sender.selected =!sender.isSelected;
}
As I understand this:
If the UIButton is not selected - then tapping the button will make it UIControlState selected (UIControlStateSelected)
if the button is selected this will reverse the above line of code and change it's state to: (UIControlStateNormal) - right?
As far as I understand this operator - it does the opposite. So in the example of our cards araay:
If it does exist - do nothing. (Method won't run) if it doesn't (not) exist then it will create an array of cards..
My problem is just when I am reading these for of lines of code that include the not operator - sometimes it isn't 100% clear what it's doing. Is there an *easy* way to rethink of this operator for better understanding when writing / reading code?
P.S: Sorry for all the n00b questions of late - my first few months of being an actual iOS developer is providing a steep learning curve. Then again I am trying to get up to speed ASAP so maybe that's why I am stumbling a little?
Thanks again all!