简体   繁体   中英

Where are the synthesized ivars stored?

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

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

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM