I'm going through lecture 2 of Stanford's iOS iTunes course and stuck on the keyword self
. It appears in two different methods and I'm having trouble understanding what self
is referring to exactly - and, why we need to include self
at all.
We have already created a class called Card and are now creating a new class called Deck to manage a collection of cards. I'm especially not sure about the function of self
in the case of self.cards
in the addCard: atTop:
method. I'm just not sure why self is used. Why isn't it inferred and what does it refer to?
It also appears in the second method, addCard:
and I'm again not sure what self refers to. Does it just refer to addCard:? If so, why is it necessary to refer back to self?
Would really appreciate any help.
Deck .h File
#import <Foundation/Foundation.h>
#import "Card.h"
@interface Deck : NSObject
- (void)addCard:(Card *)card atTop:(BOOL)atTop;
- (void)addCard:(Card *)card;
- (Card *)drawRandomCard;
@end
Deck .m File
#import "Deck.h"
@interface Deck()
@property (strong, nonatomic) NSMutableArray *cards; // of Card
@end
@implementation Deck
- (NSMutableArray *)cards
{
return _cards;
}
- (void)addCard:(Card *)card atTop:(BOOL)atTop
{
if (atTop) {
[self.cards insertObject:card atIndex:0];
} else {
[self.cards addObject:card];
} }
- (void)addCard:(Card *)card
{
[self addCard:card atTop:NO];
}
- (Card *)drawRandomCard { }
@end