2

If i don't use an ivar for properties, but do this:

@interface someClass : NSObject

@property (nonatomic, retain) NSArray * someArray;

@end

@implementation someClass

@synthesize someArray = _someArray;

- (void) someMethod
{
    if( self.someArray == nil ){
        // True on the first call?
    }
}


@end

The first time I check self.someArray it returns nil, as does _someArray, but is this guaranteed? I read only that ivars are guaranteed to be nil, and since I don't declare a ivar (_someArray is not an ivar), I am not sure if it will be nil everywhere and every time.

jscs
  • 63,694
  • 13
  • 151
  • 195
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179

3 Answers3

4

It's always nil. Objective-C initialises all the variables in a class to nil when it is allocated. Synthesised ivars follow the same rules.

joerick
  • 16,078
  • 4
  • 53
  • 57
3

Properties are backed by instance variables if they are synthesized automatically -- so yes, by default such properties will return nil.

mipadi
  • 398,885
  • 90
  • 523
  • 479
2

Yes, all properties, ivars and static variables have always been defined to be initialized to nil. Now with ARC this carries over to __strong stack variables (__strong being the default for all object pointers).

Joshua Weinberg
  • 28,598
  • 2
  • 97
  • 90