How can AnyObject have properties?

Hi


I saw this confusing code. A variable is declared as sender: AnyObject, then the code refers to sender.tag.



    @IBAction func loginAction(_ sender: AnyObject)
    {
        /
        /
        guard
            let newAccountName = usernameTextField.text,
            let newPassword = passwordTextField.text,
            !newAccountName.isEmpty &&
                !newPassword.isEmpty else {
              
                    let alertView = UIAlertController(title: "Login Problem",
                                                      message: "Wrong username or password.",
                                                      preferredStyle:. alert)
                    let okAction = UIAlertAction(title: "Foiled Again!", style: .default, handler: nil)
                    alertView.addAction(okAction)
                    present(alertView, animated: true, completion: nil)
                    return
        }
  
        /
        usernameTextField.resignFirstResponder()
        passwordTextField.resignFirstResponder()
  
        /
        if sender.tag == createLoginButtonTag {
     

etc


If I use the context menu item for the tag it takes me to this line in UIView.h, part of the declaration of @interface UIView

@property(nonatomic)                                 NSInteger tag;                // default is 0

How is that connected with sender: AnyObject?


The code comes from https://www.raywenderlich.com/147308/secure-ios-user-data-keychain-touch-id?utm_source=Swift_Developments


Does anyone know what is happening here?

Accepted Reply

AnyObject has special "polymorphic" behavior that follows a pattern used in Obj-C.


If you have a value of type "AnyObject", the compiler will allow you to invoke any method, or access any property, belonging to any class, provided that the compiler is aware that some class in your app implements the method or property.


This simplifies some code such as the above action method, where "sender" could be any of several kinds of object. If the object doesn't have a "tag" property, the code will crash, but if you first cast the object to (say) UIView, and the cast failed, you don't have much choice but to crash the app anyway.


I don't recommend following this AnyObject pattern, and I wish there was a way of turning it off in Swift, but there it is.

Replies

AnyObject has special "polymorphic" behavior that follows a pattern used in Obj-C.


If you have a value of type "AnyObject", the compiler will allow you to invoke any method, or access any property, belonging to any class, provided that the compiler is aware that some class in your app implements the method or property.


This simplifies some code such as the above action method, where "sender" could be any of several kinds of object. If the object doesn't have a "tag" property, the code will crash, but if you first cast the object to (say) UIView, and the cast failed, you don't have much choice but to crash the app anyway.


I don't recommend following this AnyObject pattern, and I wish there was a way of turning it off in Swift, but there it is.

In fact, login will be a button, hence with a tag.


But in the declaration of the IBAction, you may have noticied that you can select anyObject or UIButton as sender.


It works, because when you call, you pass in fact a UIButton.

But I personally prefer to write


@IBAction func loginAction(_ sender: UIButton)

Yes, Claude31, I prefer to be as type specific as I can in IBActions. I'm guessing this is a guy with a long Obj-C history who is not fully on-board with strong typing.

Thanks for giving me the bad news. 😉 I agree. I wish that were not available. I hope a later version of Swift deprecates that behaviour.

There is something to be said for having a long history. Strong typing is in vogue right now. Apple strongly promotes it while heavily using dynamic types for its own code. Developers want to code like Apple tells them to and flock to it. But strong typing has been in vogue before and will fall out of favour again. I can absolutely guarantee it.