64-bit Objective-C should allow hidden ivar declarations

Originator:jens.ayton
Number:rdar://5581983 Date Originated:06-Nov-2007 02:37 AM
Status:Duplicate/5277913 Resolved:
Product:Developer Tools Product Version:
Classification:Enhancement Reproducible:
 
Summary: 
In the 64-bit non-fragile Objective-C runtime, synthesized properties can use ivars not declared in the interface block. However, it is not possible to use non-property instance variables this way. There is no logical reason, in a non-fragile runtime, to require that private instance variables must be declared in the public interface; this requirement is merely an artefact of the way the fragile runtime works.

Steps to Reproduce:
1. Create a new ObjC class. (Sample provided.)
2. In its interface, do not declare any instance variables.
3. In its implementation, add an instance variable declaration.
4. Add a method attempting to use the ivar declared in (3).
5. Compile.

Expected Results:
Successful compilation. The instance variable(s) declared in the @implementation block should be added to those declared in the @interface, if any, and be accessible as normal.

Actual Results:
This usage is not supported.
error: inconsistent instance variable specification
error: ‘testIvar’ undeclared (first use in this function)


Regression:
n/a

Notes:
My specific goal is to have a property on an immutable class which is read-only and has no ivar declaration in the header (when compiled in 64-bit mode). It is of course necessary to write the property once, on init. With explicit ivar declarations, this is easy. Without, it is not possible to write to the ivar generated by the @synthesize directive, nor is it possible to declare the ivar privately as per the above.

My workaround has been to add a second property declaration of the form @property (readwrite, setter=priv_initValue:) to an internal category. This isn’t particularly satisfactory, though.

Comments

Adding instance variable declaration to implementation

See the first paragraph and code sample under Class Implementation in the Objective-C docs:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocDefiningClasses.html

@implementation ClassName : ItsSuperclass { instance variable declarations } method definitions @end

Step 3

I don't understand what step 3 is: how do you add an instance variable declaration to an implementation?

By charles.parnot at Sept. 14, 2009, 4:39 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!