openfrog
openfrog

Reputation: 40735

Why would an auto-synthesized property's ivar remain nil even after the property is set?

I discovered an odd problem.

In this example Xcode 5.0.2 with iOS 7.0 on the "iPhone Retina (4-inch)" simulator something happens that should not be possible.

A property in a UIView subclass has been defined as:

@property (nonatomic, strong) EAGLContext *context;

There is no custom getter or setter. So _context ivar should be created.

It seems the ivar exists, but yet when I assign something the ivar remains nil.

self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];

// breakpoint:
// "po [self context]" returns proper object.
// "po _context" returns nil

How can this be possible?

To exclude collisions I refactored the property to "context123" but the same result. LLDB debugger will say that _context123 is nil.

My answer to this question will follow immediately. It is an interesting problem which others may find useful to know.

Upvotes: 0

Views: 183

Answers (1)

openfrog
openfrog

Reputation: 40735

The reason is that a subclass has accidentally overridden this property. This causes the creation of a colliding ivar in the subclass, whose value shadows the ivar in the superclass.

Upvotes: 1

Related Questions