-2

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
cheznead
  • 2,589
  • 7
  • 29
  • 50

3 Answers3

3

In Objective-C, the notation for a method call is

[object message:argument]

If you want to send a message to cards, you need to know which instance of Deck whose cards element you want to refer to.

So when you say self.cards it says "I want the cards element in the current instance of Deck".

So self.cards is the "object" in the Objective-C call.

Almo
  • 15,538
  • 13
  • 67
  • 95
  • 1
    Good explanation. For someone who is not familiar with object oriented programming, this can frequently trip you up. When you write init, you are actually creating an instance of an object. If you want that object to do something you need to refer to that instance. It took me a while to figure that out. – JScarry Nov 26 '14 at 16:12
  • Arent you always working with the current instance of Deck while in this class though? Why do you need to include self? Like why not just have written [cards message:argument] instead? – cheznead Nov 26 '14 at 16:40
  • Not if you're in a "class method". These start with a "+" instead of a "-". Also, what if you're in a function of Deck that takes a Deck and copies it over. Then you have `self.cards` and `argumentDeck.cards`. – Almo Nov 26 '14 at 16:41
  • ok, get it somewhat better now. Read through other linked pgs too. thanks for your help. – cheznead Nov 26 '14 at 17:02
1

When you have a class like you put, you can't just call it's methods without an object. First you have to do this:

Deck* myDeck = [[Deck alloc] init];

Later, if you do this:

[myDeck addCard:myCard atTop:YES];

It calls the method addCard:onTop: on myDeck. In the code in the method, self now refers to myDeck, allowing us to add cards to the right deck. If you call the method on a different instance of Deck later, self will refer to that instance.

Linuxios
  • 34,849
  • 13
  • 91
  • 116
0

Excerpt from Wikipedia Article: this (computer programming):

this, self, and Me are keywords used in some computer programming languages to refer to the object, class, or other entity that the currently running code is part of. The entity referred to by these keywords thus depends on the execution context (such as which object is having its method called). Different programming languages use these keywords in slightly different ways. In languages where a keyword like "this" is mandatory, the keyword is the only way to access data and methods stored in the current object. Where optional, they can disambiguate variables and functions with the same name.

Black Frog
  • 11,595
  • 1
  • 35
  • 66