Since xcode 4.5 all properties will be synthesized automagically.
Objective-C accessors also available without @synthesize
-
12-12-2021 - |
Frage
Consider following code:
@interface TestClass ()
@property NSString *privateProperty;
@end
Now, as I learned, to make to compiler also create the accessors, I have to add the @synthesize directive in the @implement-part.
But also, if don't add the directive, following message works:
[self setPrivateProperty:@"foo"];
From my understanding, this should lead to an error explaining me, that the selector is unknown. Why does this setter (and I think also the getter) still exist?
Lösung
Andere Tipps
The @property
declaration declares the existence of the getter and setter methods. The @synthesize
declaration creates implementations of those methods.
Before Xcode 4.5, you'd have got a compiler warning for omitting the @synthesize
and a runtime crash - both caused by there being no implementation for the methods you'd implicitly declared.
As of Xcode 4.5, the implementation of the methods is automatically synthesized unless you specify otherwise (@dynamic
).
It's a new feature introduced in a recent version of XCode: it auto-syntetize the property, creating an _propertyName ivar to store it.