Hi all,
So this morning I took on the first homework set by the CS193p Stanford iOS programming course (Winter 2013)
The homework was to make the card "flip" through different suit / ranks.
Now this is how I first tried the challenge:
So the property Playingcard deck inherits from another class deck - which has the method drawRandomCard.
Deck inherits from another class Card.
Now the above code would give my NSLog statement (null) and for some reason and I couldn't access my deck property directly without the
This version of the code works perfectly:
My understanding of self:
It calls the object calling the method. Its calling itself.
Questions:
1. Why did the first version of the code not work?
2. Can someone try explain self a little better for me?
If I had understood self - I would have completed the homework all on my own - however I took a look at an example and I was only missing the self keyword...so close!!

So this morning I took on the first homework set by the CS193p Stanford iOS programming course (Winter 2013)
The homework was to make the card "flip" through different suit / ranks.
Now this is how I first tried the challenge:
Code:
#import "CardGameViewController.h"
@interface CardGameViewController ()
@property (weak, nonatomic) IBOutlet UILabel *flipsLabel;
@property(nonatomic) int flipCount;
@property (nonatomic, strong) PlayingCardDeck *deck;
@end
@implementation CardGameViewController
@synthesize deck;
- (PlayingCardDeck *)deck
{
if (!deck) deck = [[PlayingCardDeck alloc]init];
return deck;
}
-(void)setFlipCount:(int)flipCount
{
_flipCount = flipCount;
self.flipsLabel.text = [NSString stringWithFormat:@"Flips: %d", self.flipCount];
}
- (IBAction)flipCard:(UIButton *)sender
{
sender.selected = !sender.isSelected;
[sender setTitle:[_deck drawRandomCard].contents forState:UIControlStateSelected];
self.flipCount++;
NSLog (@"%@", [self.deck drawRandomCard].contents);
}
@end
So the property Playingcard deck inherits from another class deck - which has the method drawRandomCard.
Deck inherits from another class Card.
Now the above code would give my NSLog statement (null) and for some reason and I couldn't access my deck property directly without the
Code:
@synthesize deck;
This version of the code works perfectly:
Code:
#import "CardGameViewController.h"
@interface CardGameViewController ()
@property (weak, nonatomic) IBOutlet UILabel *flipsLabel;
@property(nonatomic) int flipCount;
@property (nonatomic, strong) PlayingCardDeck *deck;
@end
@implementation CardGameViewController
- (PlayingCardDeck *)deck
{
if (!_deck) _deck = [[PlayingCardDeck alloc]init];
return _deck;
}
-(void)setFlipCount:(int)flipCount
{
_flipCount = flipCount;
self.flipsLabel.text = [NSString stringWithFormat:@"Flips: %d", self.flipCount];
}
- (IBAction)flipCard:(UIButton *)sender
{
sender.selected = !sender.isSelected;
[sender setTitle:[self.deck drawRandomCard].contents forState:UIControlStateSelected];
self.flipCount++;
NSLog (@"%@", [self.deck drawRandomCard].contents);
}
@end
My understanding of self:
It calls the object calling the method. Its calling itself.
Questions:
1. Why did the first version of the code not work?
2. Can someone try explain self a little better for me?
If I had understood self - I would have completed the homework all on my own - however I took a look at an example and I was only missing the self keyword...so close!!