Question

Really, why most of people use self.something for almost everything when code works without it?

for example:

-(void)viewDidLoad {

self.mylabel.text = [NSString stringWithFormat:@" %@", someVariable];

}

The code works the same way without self.

I have tried to see what happens without self and it always works.

Was it helpful?

Solution

Using self. means that you are using the getter/setter, and not using it means that you are accessing the instance variable directly.

This question has been treated a lot here, but summarising:

ALWAYS create a @property for every data member and use “self.name” to access it throughout your class implementation. NEVER access your own instance variables directly.

Properties enforce access restrictions (such as readonly)

Properties enforce memory management policy (strong, weak)

Properties provide the opportunity to transparently implement custom setters and getters.

Properties with custom setters or getters can be used to enforce a thread-safety strategy. Having a single way to access instance variables increases code readability.

Source:

Best Practices fr Obj-C

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top