Is there a way to make this call in dot notation?:
[someSwitch setOn:YES animated:YES]
Is there a way to make this call in dot notation?:
[someSwitch setOn:YES animated:YES]
Make a custom class for someSwitch:
@interface MySwitch : UISwitch
@property (assign) BOOL animatedOn;
@end
@implementation MySwitch
-(BOOL) animatedOn { return [self isOn]; }
-(void) setAnimatedOn:(BOOL)inOn { [self setOn:inOn animated:YES]; }
@end
Then use it:
someSwitch.animatedOn = YES;
Setting a property through dot notation is limited to a single argument. However, getters and setters need not map to actual members. All the Apple setters with an animated:
variant default to not animating when used with dot notation.
@drawnonward's answer is a good one. The question is why you'd want to do that.
The beauty of dot notation for accessing the getters and setters of synthesized object properties is that you can THINK of the dot-notated properties as data fields. Behind the scenes there's method call stuff happening, but as you write, it feels like you're talking about the object's data fields directly.
I've watched several new iPhone developers (including the one I'm training now) getting really confused about when to dot-notate and when to do an [object message]
. The bottom line of it is, dot-notation is for accessing data fields (while knowing that it's really a convenient piece of syntactical sugar around synthesized getter and setter methods) and method calls are for instructing objects to do something. And obviously "setValue" is a possible something to do, and that's totally valid too.