By binnyb

2010-11-02 17:43:58 8 Comments

When should I be using the self expression in my iphone development applications? say i have 2 fields: UITextField *text1; and NSString *str1; retained and synthesized.

when i am accessing either of these 2 fields, when should i and when should i not use self.text1 and self.str1 ?


@theiOSDude 2011-03-09 13:05:00

if you "synthesize" the variable, you should "self." the variable. little rule of thumb

@Ben Scheirman 2010-11-02 17:50:10

In your example you aren't directly accessing instance variables when you use self, instead you're accessing the properties you've defined.

Consider this example:

@interface Foo : NSObject {
   NSString *_bar;

@property (nonatomic, retain) NSString *bar;


@implementation Foo
@synthesize bar = _bar;
-(void)baz {
   _bar = @"ivar";  //accessing the ivar = @"property"; //accessing the ivar via the property


In general if you're using properties, there's little reason to utilize the ivar. This has the added benefit of automatically retaining & releasing values for you.

But other cases exist when your properties will have a readonly modifier. In these cases it's necessary to directly access your ivars in order to set their values.

@Wade Mueller 2010-11-02 19:35:58

I'm confused. Where is _foo and foo declared? Should they really be _bar and bar in your baz method?

@Ben Scheirman 2010-11-02 19:53:43

sorry, I made a typo in my own example. corrected

@Max Seelemann 2010-11-02 19:03:17

There are certain circumstances where it's generally discouraged to use the self.-expression to access a property. Normally you always use self for any access of a property. It's the most secure and uncomplicated way. Especially if you used retain, then memory management will be done for you.

The two exceptions from this rule:

  • Any init method.
  • In dealloc.

In both cases you are dealing with an partially initialized object. There are some side effects that may occur when using setters or getters here -- because they are methods and hence may be overridden.

For example, take a class A with a property foo that has been subclassed by class B. The subclass B adds an property bar and overrode the setter for foo. Now your init-method calls setFoo:, because you used = ... with some initial value. The subclass, however, also accesses the value of bar in this setter. But in this case, it may happen that bar has never been initialized and points at some arbitrary data. Calling a setter in init my cause crashes, although the probability may not be too high in your own code.

@lewiguez 2010-11-02 17:51:59

It's also a good idea to use self within a method call sometimes if you have a custom getter. The managedContext object within a Core Data-using application is a good example. If you refer to it by self.managedContext, you can override and set the object to what it needs to be if it's nil. Refer to the code generated by XCode when creating an application that uses Core Data.

Here is an example of the code generated by XCode, actually:

@interface YourAppDelegate : NSObject <UIApplicationDelegate>
    NSManagedObjectContext *managedObjectContext_;

@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;

@implementation ContractionTimerAppDelegate

 Returns the managed object context for the application.
 If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext {

    if (managedObjectContext_ != nil) {
        return managedObjectContext_;

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext_ = [[NSManagedObjectContext alloc] init];
        [managedObjectContext_ setPersistentStoreCoordinator:coordinator];
    return managedObjectContext_;


@Vilx- 2010-11-02 17:48:43

I don't know anything about objective-c, but this looks a lot like this keyword from other languages (like C++, C#, Java, PHP, and others). If so, then my advice is to use it always. That way, if you ever (accidentally) define a local variable with the same name, your code won't break.

However, I must also add, that this is somewhat of a religious debate with a history of flamewars in programmer communities. So take this advice with a grain of salt and use whatever seems to make most sense to you. Just be consistent about it.

@David Gelhar 2010-11-02 18:03:16

But note that it's not just a style/maintainability issue in objective-C -- when using a property which has getter/setter methods, omitting the "self" bypasses the getter/setter; there's a real semantic difference.

@binnyb 2010-11-02 18:05:49

that is exactly what i was looking for, nice

@Vilx- 2010-11-02 19:28:13

@David Gelhar - sorry. Told you I didn't know. :P

@jer 2010-11-02 17:45:36

self is not a keyword, it is an expression. Additionally, you use it any time you want to refer to a method or property on yourself, or yourself directly. By "yourself" I am of course, referring to the instance of the class you are operating in.

@binnyb 2010-11-02 17:48:33

so it seems like it is identical to the Java this expression, which is not always required unless dealing with variables with the same name

@Jed Smith 2010-11-02 17:53:15

You are correct.

@David Gelhar 2010-11-02 17:59:42

One important difference is that self.str1 = @"abc" will use the accessor method (setStr1:) but str1 = @"abc" will not. When using properties with the retain attribute, this difference is critical. So, it's really not "identical to Java this".

Related Questions

Sponsored Content

4 Answered Questions

[SOLVED] What is a provisioning profile used for when developing iPhone applications?

  • 2010-07-29 12:56:57
  • user174761
  • 181823 View
  • 130 Score
  • 4 Answer
  • Tags:   iphone ios

8 Answered Questions

[SOLVED] What does the "__block" keyword mean?

92 Answered Questions

20 Answered Questions

[SOLVED] What is the purpose of the word 'self', in Python?

  • 2010-04-25 20:22:28
  • richzilla
  • 780335 View
  • 1023 Score
  • 20 Answer
  • Tags:   python class oop self

7 Answered Questions

[SOLVED] Version vs build in Xcode

  • 2011-07-27 21:48:15
  • chris
  • 139026 View
  • 638 Score
  • 7 Answer
  • Tags:   ios xcode

8 Answered Questions

[SOLVED] iOS 6 apps - how to deal with iPhone 5 screen size?

  • 2012-09-12 21:40:08
  • jturolla
  • 161073 View
  • 264 Score
  • 8 Answer
  • Tags:   iphone ios iphone-5

3 Answered Questions

[SOLVED] When to access properties with 'self'

4 Answered Questions

[SOLVED] When to access property with self and when not to?

3 Answered Questions

[SOLVED] Using self in method call

10 Answered Questions

[SOLVED] How much does it cost to develop an iPhone application?

Sponsored Content