is it OK to assign to a property that is readonly?
If you @synthesize coordinate;
, then the backing ivar will be named coordinate
. If the property is auto-synthesized, it will be named _coordinate
.
If your intent is to assign to the underlying instance variable within the initializer, that is fine (e.g. coordinate = c
or _coordinate = c
).
If you want to set it using the setter (e.g. [self setCoordinate:c]
or self.coordinate = c;
you would need to implement the setter yourself or synthesize its definition (e.g. by declaring the property readwrite in your class continuation).
should not coordinate have self before it? (e.g., self.coordinate = c).
No, it should not. Use direct access in a partially constructed state, such as an initializer or -dealloc
.
Given the properties:
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy, readwrite) NSString * title;
Your initializer should take the form:
- (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t
{
self = [super init];
if (self)
{
_coordinate = c;
_title = t.copy; // note: copy/ownership semantics match property declaration
}
return self;
}
@end