I take for example the UIButton
interface.
Here the first rows of @private
definition :
@private
CFMutableDictionaryRef _contentLookup;
UIEdgeInsets _contentEdgeInsets;
UIEdgeInsets _titleEdgeInsets;
And here 2 of these ivar, that are defined as properties:
@property(nonatomic) UIEdgeInsets contentEdgeInsets;
@property(nonatomic) UIEdgeInsetstitleEdgeIns开发者_如何转开发ets;
However these 2 properties are not defined on the ivars i found in private method (which have suffix _
).
I'm not sure to understand how could be implemented setter and getter for these 2 properties to refer to the private ivars.
And a second question... i used to create properties for ivar, thus, if i have an ivar FOO
i can create a @property
for FOO
. Is it a normal behavior create property for a non existing ivar ? (in this case contentEdgeInsets
is not an attribute for this class... on the contrary _contentEdgeInset
is defined in @interface
and this's a valid ivar). Ok what i missed with this argument ?
When you @synthesize these properties you do so like
@synthesize contentEdgeInsets = _contentEdgeInsets;
^property name ^iVar name
Check out the Property Implementation Directives section in the documentation.
By default, a property will use the ivar whose name is the same as that of the property, but it's also possible to specify an ivar of a different name. Do this in your @synthesize statement in the class implementation.
In the modern runtime, used pretty much everywhere at this point, you don't actually have to declare the ivar at all -- if you synthesize accessors for a property and there's no matching ivar, the runtime will provide one.
Finally, properties with @dynamic rather than @synthesized accessors don't necessarily need an ivar at all -- you're providing the accessors in this case, so you're free to derive the value of the property however you like.
精彩评论