Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
P M
7,320 PointsShould I strongly type sender in IBAction?
I am wiring an IBAction from a UIBarButton. Should I keep the sender type as id? In this case, I've seen some examples of typecasting the sender to the type of the caller. But can I just explicitly type (UIBarButton *)sender? What is the established best practice? In what case would I need to leave it as id and treat it as it knows what is doing, in what case should I type cast id later or just explicitly strongly type it in the method name (like (UIButton *)sender)?
1 Answer
Colin Jackson
4,829 PointsIn situations like that, I'd say the best approach is to trust Xcode. If it isn't asking you to typecast sender, that's probably because Apple doesn't think it's important to do so.
Personally, I wouldn't typecast sender unless I had a very specific reason why I wouldn't want that method to be called by something else.