I would like to prevent the user of one of my Objective-C objects from setting a property of a property of the object. Just like it is not possible to set the origin
of a frame
of UIView
. I need this for the exact same reason: to be able to manage some side effects on the setter of the property.
Example:
@interface Time : NSObject
@property (nonatomic) NSInteger hours;
@property (nonatomic) NSInteger minutes;
@end
@interface Watch : NSObject
@property (nonatomic) Time *time;
@end
It now should not be possible to set the minutes directly by calling:
watch.time.minutes = 15;
…rather than:
Time *time = watch.time; // [watch.time copy]?
time.minutes = 15;
watch.time = time; // Here, in setTime:, I can implement some side effects
When trying to set a UIView
frame
's size, the compiler complains like so: "Expression is not assignable". How could Apple’s framework developers have done this? Can this only be done with structs? How can I achieve a similar thing?