1

Possible Duplicate:
Difference between class property mVar and instance variable self.mVar

I am new to developing in Objective-C and I couldn't quite figure out what the difference is between the following:

First let me explain my situation. I've got an NSMutableArray, and I created and outlet for it in my .h file. Now when I assign an array to it as

self.myMutableArray=myArray

I get an error; However just

myMutableArray=myArray

works fine.

I am not interested in the resolving of the error. I just want to know what is the difference when putting self in front of something? And why I am able to use the variable also without self and what restrictions that brings with it?

Community
  • 1
  • 1
rajomato
  • 1,167
  • 2
  • 10
  • 25
  • Show us the code, especially where your property is declared in the header. – Peter DeWeese Nov 06 '11 at 21:17
  • 4
    Note that there’s no such thing as `self.instanceVariable`: it’s either `self->instanceVariable`, `instanceVariable` or `self.declaredProperty`. –  Nov 06 '11 at 21:20
  • Here is a link that talks about this very topic: [http://answers.oreilly.com/topic/1193-calling-self-object-rather-than-calling-the-object-directly-in-objective-c/] – syclonefx Nov 06 '11 at 21:36
  • Which covers @Bavarious's suspicion that this question is about an ivar _without_ a corresponding property. – jscs Nov 06 '11 at 21:41
  • 1
    Seriously, you couldn't have searched for this? – Dave DeLong Nov 06 '11 at 22:15

2 Answers2

5
self.property = value;

is equal to:

[self setProperty:value];

That is to say, the declaration using self. goes through the object's accessor method, rather than using direct access.

They have different causes and effects. Perhaps the most notable is that direct access will often leads to reference count issues (leaks/zombies) if not used with care. The accessor is responsible for handling memory management - if synthesised, or if you implement it yourself.

The General Rule: You should favor using the accessors (self.blah = thing;) over direct access (blah = thing;) until you know when and why you would make exceptions to this rule.

The immediate exception: There is one exception to the general rule: Do not use the accessors in partially constructed states, such as the object's initializer or dealloc. In those cases, use direct access:

- (id)init
{
  self = [super init];
  if (0 != self) {
    things = [NSArray new];
  }
  return self;
}

- (void)dealloc << not needed with ARC, in this case
{
  [things release], things = 0;
  [super dealloc];
}

Update

Describing Bavarious' suspicion of the error:

It sounds like you have declared an instance variable, but have not declared an associated property or proper accessors (e.g. the setter). Here's a breakdown of a class' declaration with ivars and properties:

@interface MONObject : NSObject
{
@private
  NSMutableArray * myMutableArray; << declares an instance variable
}

// the property declaration:
@property (nonatomic, retain, readwrite) NSMutableArray * myMutableArray;
// adds the accessors:
//   - (NSMutableArray *)myMutableArray; << getter
//   - (void)setMyMutableArray:(NSMutableArray *)arg; << setter
// to the class' interface.

@end


@implementation MONObject

// @synthesize below generates the accessors for the property
// myMutableArray, using "myMutableArray" ivar by default.
@synthesize myMutableArray;

- (void)zumBeispiel
{
  NSUInteger count = 0;

  // direct access:
  count = [myMutableArray count];
  // is equal to:
  count = [self->myMutableArray count];

  // Access via the getter:
  count = [self.myMutableArray count]; << equal to [self myMutableArray]
  // is equal to:
  count = [[self myMutableArray] count];
}

@end
justin
  • 104,054
  • 14
  • 179
  • 226
  • You might want to write a brief paragraph explaining declared properties vs. instance variables. I have the impression that the OP hasn’t declared properties at all. –  Nov 06 '11 at 21:30
  • @Bavarious updated - edit away, if you like. cheers – justin Nov 06 '11 at 21:46
  • In your exception you wrote things = [NSArray new]; whereas you state that if assigning a value you should always use self. So why dod you just use things= and not self.things= ? – rajomato Nov 08 '11 at 17:49
  • @iJatrat Because you should not use the accessors in `init`. That program demonstrates "The Immediate Exception" to the "General Rule". The exception is that you should avoid using the class' (public) instance methods (including, but not limited to, accessors) in partially constructed states (which `init...` and `dealloc` qualify as). For details as to why this is the exception, see my answer here: http://stackoverflow.com/questions/5932677/initializing-a-property-dot-notation/5932733#5932733 – justin Nov 08 '11 at 17:58
  • @Justin thanks a lot! Cleared up a lot!! – rajomato Nov 09 '11 at 15:10
0

self.variable is a property defined in the .h file like so

@property (retain) NSString *variable;

and

@synthesize variable;

in the .m

whereas just "variable" is defined as

@interface MyClass: MyParent {
     NSString *varaible;
}

in the .h

macintosh264
  • 983
  • 2
  • 11
  • 27