Isaac Majdi
Isaac Majdi

Reputation: 217

Confusion when using the self keyword

I have a few questions about this code:

#import "ViewController.h"
@interface ViewController ()
@property (copy, nonatomic) NSArray *myArray;
@end

@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myArray= @[@"one", @"two", @"three", @"four"];
}

I know the self keyword means that's method will be sent to the receiver of that message, and I know when and how to use it if i was creating a simple command utility code, but when learning about creating an iPhone app and using the storyboard every time I see the self keyword it confuses me, I don't know which object is the receiver 2nd question, I know that @[@"object", @"object"];is quick way to create an NSArray object, but "myArray" is a property not an NSArray object. please explain, thank you.

Upvotes: 0

Views: 120

Answers (3)

Caleb
Caleb

Reputation: 124997

myArray is. The name of a property, as you say. By default, the compiler will create an instance variable called _myArray that's used by that property. In any case, because you've declared the property to be of type NSArray * (and not readonly) you can assign an array to it.

The self keyword is simply a pointer to the "current" object, some instance of the class that you're writing code for when you type "self". If you assign something to self.myArray, the class in which you make that assignment needs to have a myArray property. In your code example, self is a pointer to the particular instance of ViewController that just loaded its view. None of that changes when you're writing an iPhone apportion using a storyboard. self is always the object receiving the method in which self is used. (Note: when used in a class method, i.e. one that starts with +, the object receiving the method is the class itself, not an instance of the class. Don't worry about this if it sounds confusing -- it doesn't come up all that often.)

If you find an expression like self.myArray confusing, it may help to know that property syntax is just shorthand for a method call. Also, self in an expression like that isn't special at all. The syntax is foo.bar where foo can any object pointer (including self) and bar can be any property of the object pointed to by foo. The expression translates directly to either [foo bar] or [foo setBar:...], depending on whether you're reading or assigning a value to the property. So,

self.myArray = @[a, b, c];

means exactly the same as:

[self setMyArray:@[a, b, c]];

Upvotes: 1

Matic Oblak
Matic Oblak

Reputation: 16774

"self" is a pointer to object to which the method belongs. In your case you may have many instances of ViewController and each of them can call viewDidLoad and in that method "self" is the pointer to the instance itself. To look at it from the C perspective you could create a method using pointer to function where you would also send own pointer to the function called "self", for instance void foo(MyClass *self); to call it then myClassInstance->foo(myClassInstance); this procedure kind of simulates methods but you always need to send instance pointer as well (self). So there should be no confusion as to what "self" is except it is more commonly known by "this" keyword. It is just a reference to the object on which the method is being called.

The property is just a convenience of usually 2 methods called getter and setter. When creating a property @property NSArray *myArray you will actually generate a pointer usually NSArray *_myArray; then a getter:

 - (NSArray *)myArray {
    return _myArray;
 }

And a setter

- (void)setMyArray:(NSArray *)myArray {
    _myArray = myArray;    
 }

How getters and setters are created depends on property attributes (strong, weak, readonly...). You may even override any of these methods or both. So as you stated "but "myArray" is a property not an NSArray object" a property can have truly any type.

Upvotes: 1

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

self always corresponds to the self class. So every instance of a class will have its own self.

In your case myArray is a property of Class ViewController. Here you can refer to the property by using self.myArray or _myArray.

In the following statement you are creating an array with four strings and adding it to myArray.

self.myArray= @[@"one", @"two", @"three", @"four"];

Your wordings in the question :

I know the self keyword means thats method will be sent to the receiver of that message

is valid for a method call where you use it as :

[self aMethod];

Even in this you are calling a method which is a part of the class. If you call any method which is not of the current(self) class then you call it by using that class' object name as:

SomeClass *someClassObject = ...
[someClassObject itsMethod];

Upvotes: 1

Related Questions