0

In the following example, is the line NSObject *_propertyName; required?

.h

@interface  ClassName
{
    NSObject *_propertyName;
}

@property (nonatomic, retain) NSObject *propertyName;

@end

.m

@implementation  ClassName

@synthesize propertyName = _propertyName;

@end

I find that if I exclude NSObject *_propertyName; but keep @synthesize propertyName = _propertyName; everything works. Here's an example of what I'm talking about:

.h

@interface  ClassName

@property (nonatomic, retain) NSObject *propertyName;

@end

.m

@implementation  ClassName

@synthesize propertyName = _propertyName;

@end

I've tested and seen that the property still works. I nearly always see code that includes the line NSObject *_propertyName;. Is there something I'm missing here?

Nate
  • 12,963
  • 4
  • 59
  • 80
  • 1
    Please do a search before posting. There's already a gazillion and a half questions that cover this. Here's one such: http://stackoverflow.com/questions/8330257/is-it-necessary-to-declare-ivars-in-interface-to-match-properties – jscs Jan 10 '12 at 18:21
  • 1
    Here's a search that will turn up more: http://stackoverflow.com/search?q=%5Bobjc%5D+declare+ivars+for+properties Some of these are in the Related sidebar to the right, and would have come up when you were drafting your question. – jscs Jan 10 '12 at 18:23
  • 1
    I did a lot of searching before posting this answer. None of the other questions came up. Sorry for the repost, but I really tried to find an answer. – Nate Jan 11 '12 at 06:02
  • 1
    So, in retrospect, I must have not been searching for the right phrase. I'll be more creative in the future. – Nate Jan 11 '12 at 06:15

1 Answers1

3

You're not missing anything. Starting with the newer runtimes (newer iOS Simulator, x86_64 and ARM) you no longer need to manually declare an ivar. Prior to that on i386 and PPC you had to manually declare your ivars.

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