What's the difference between AnyObject and UIbutton as sender?
Yes, both works. The difference is just that by declaring it to a button, you get a typecasted reference as UIButton instead of AnyObject (or id in Objective-C). If you did not do that you will have to do it manually inside the code.
You should prefer leaving it to AnyObject in case the action has some code you would like to call from anywhere else, rather than just the button action.
For example a refresh button, you might have to do a refresh programatically. If you have set your action parameter to UIButton, you have to send an UIButton (but why??? You are doing it programatically, right?). Whereas if it is AnyObject, you can send 'self'. Which will also make you distinguish whether the refresh was done on action or programatically.
Hope that explains. Sorry for no code. I am not into Swift coding much. Edits are welcome.
EDIT
So, even if it is AnyObject, it does have your UIButton properties. Just type cast it to use them.
If you are only ever going to use the function with a UIButton
it is best practice to declare your sender as a UIButton
. This saves you a bit of code and it also tells anyone in the future reading your code that you only expect the function to be used with a UIButton
.
Using AnyObject
or Any
will work, but you will need to do an guard let button = sender as? UIButton { return }
in order to access it as a button. This way allows you to react differently depending on what the sender actually is, but I don't recommend doing that.