6
votes

I've been reading up on the automatically synthesized ivars. My question is, "WHere are automatically they allocated?" I would have expected them to be part of self, so that I could see them in the debugger, but it seems that the only way I can see them is by invoking the accessor method (via the gdb 'po' command). Isn't there space in the class/object's struct (as there would be for an explicitly declared ivar)?

(Is there a description of the in-memory representation for a modern Objective-C object?)

Being a C guy, it makes me very uncomfortable to not to be able to see where everything is. :-P

3

3 Answers

1
votes

Looks like this will tell you: How do automatic @synthesized ivars affect the *real* sizeof(MyClass)?

I am a C guy at heart too. Why bother using these auto generated ones? I like looking at a class and seeing what it holds onto in terms of data.

Interesting: Neat how they took the 64 bit change to make things better. http://www.sealiesoftware.com/blog/archive/2009/01/27/objc_explain_Non-fragile_ivars.html

1
votes

They are added to the objective-c object (which is a C structure) no different to a regular ivar, so for example:

@interface TestObject : NSObject {

}

@property (nonatomic, assign) int theInt;

@end

@implementation QuartzTestView

@synthesize theInt;

@end

You can refer to theInt ivar directly (not through property accessors) either:

- (void)someMethod {
    theInt = 5;
}

OR

- (void)someOtherMethod {
    self->theInt = 10;
}
0
votes

See http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html - using the modern runtime an instance variable "will be synthesized for you". It can be nice to add a variable yourself instead though (so that you can see it when debugging in self), however you have to be careful not to do direct assignments to the instance variable for retain or copy based properties.