Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jlw2387

macrumors member
Original poster
Nov 13, 2010
64
0
I have been working through an iPhone development book and I ran across this bit of code:

Code:
-(IBAction)switchChanged: (id)sender {
     UISwitch *whichSwitch = (UISwitch *)sender;
...
I dont really understand the why there is a * before the ). Can someone explain this?
 
Last edited by a moderator:
Code:
(UISwitch *)

tells the compiler to treat the following item as a pointer to a UISwitch object.

Why is this needed? Because:

Code:
(id)sender

doesn't tell the compiler what type of variable the name "sender" is referring to.
 
I think I understand. So even if I know what class of object will be sent to "sender", this tells the compiler what class it is. Can I access sender's class properties directly or is the only way to do it is to copy to a new object like they did in the example?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.