parentParserDelegate in RSSItem.m


Don’t know if anyone else has experienced this, but Xcode insists on changing this:

if ([elementName isEqual:@"item"]) [parser setDelegate:parentParserDelegate];
…to this:

if ([elementName isEqual:@"item"]) [parser setDelegate:_parentParserDelegate];

If I leave out the underscore it gives me an error that parentParserDelegate is undefined. Any idea why this is happening? It only seems to happen in RSSItem.m. I’ve checked the code against the book and against RSSChannel.m and I can’t see any relevant differences. Please help!


There is quite a bit of variety in declaring instance variables in ObjC (an annoying amount of variety). What you are looking at is something fairly recent.

Let’s say you declared an instance variable in some SourceObj.h
"@property (nonatomic, weak) RSSChannel *parentParserDelegate;"

Case 1: you do NOT have an associated “@synthesize parentParserDelegate;” in SourceObj.m
then your use of this instance variable will be:
[parser setDelegate:_parentParserDelegate];

ObjC will automatically do the synthesize for you, but !!! it’s making the instance variable start with an “_”, which apparently is the new trendy thing to do. Also, it’s all over the apple sample code so it might be an uphill battle to fight this.

Case2: you add “@synthesize parentParserDelegate;” in SourceObj.m
then this works:
[parser setDelegate:parentParserDelegate];

Oddly, the getter and setter are the same either way, whether you @synthesize or not.
getter will be:
id obj = [self parentParserDelegate];
setter will be:
[self setParentParserDelegate:someObj];

So, at this point in ObjC, it looks like @synthesize only determines what style of instance variable (with “_” or not) you want.
I could be really wrong about this so “do you own due diligence”, and please correct me if I’m spreading misinformation. It looks like a fairly recent change in Xcode/ObjC, some time after Xcode 4.3?. (I’m using Xcode 4.6.2).

Personally, I dislike my instance variables starting with “_”, it just doesn’t look nice :slight_smile: and Xcode highlights instance variables differently anyway so, where is the need for this coming from?