Simple Question

Discussion in 'iOS Programming' started by jlw2387, Nov 16, 2010.

  1. jlw2387, Nov 16, 2010
    Last edited by a moderator: Nov 17, 2010

    jlw2387 macrumors member

    Joined:
    Nov 13, 2010
    #1
    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?
     
  2. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #2
    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.
     
  3. jlw2387 thread starter macrumors member

    Joined:
    Nov 13, 2010
    #3
    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?
     

Share This Page