So let’s say I have this method that responds to a button event
- (void) myMethod: (id) sender
Is it better to use directly the function param?
NSLog(@"The sender tag is %d",sender.tag);
Or is it better to create a new object?
UIButton* myButton = (UIButton*) sender;
NSLog(@"The sender tag is %d",myButton.tag);
Why?
I’ve seen in tutorials that the preferred way in objective-c is the second one. But, in cases where you don’t need to know the type of the sender and just access its properties/methods it should be Ok to use the first way. Am I missing something?
You’re not missing anything and this isn’t a particularly important issue. You have a few options:
Sometimes that will produce warnings when compiling, depending on the type of the method parameter.
Or finally:
(These all basically do the same thing, so it’s mostly a matter of preference.)