I am trying to learn about Objective-C for iPhone development, and while going through examples about actions I often saw things like:
- (IBAction)sliderMoved:(id)sender
{
UISlider *aSlider = (UISlider *)sender;
//rest of code goes here...
}
What is the purpose of taking in type (id) and then immediately casting it to (UISlider *). Why not just have the following:
- (IBAction)sliderMoved:(UISlider *)aSlider;
{
//rest of code goes here...
}
Is there a technical reason for this, or is it simply convention, or done for readability?
Thanks in advance,
- Timo
I believe it is a convention that stems from the fact that there is not always a 1:1 mapping of UIControl to IBAction. For example, you could have a UISwitch, a UIButton, and a UITextField all mapped to a
- (IBAction)syncUI:(id)sender;method. In that case, it is impossible to specify the type.I admit that this is just my own mental justification, and there is certainly nothing preventing you from being more explicit in the type of sender if you can be. I usually am.