I am not sure why I keep getting the error Value of type '(AnyObject) -> ()' has no member 'currentTitle'

若如初见. 提交于 2021-01-29 14:10:12

问题


@IBAction func Hint(_ sender: UIButton) {
        if Action.currentTitle != Int(rightAnswerPlacement) // this is the line where I keep getting error I posted above 
        {
            Action.isHidden = true // true to hide the button
        
        }
    }
    
    @IBOutlet weak var Hint: UIButton!
    
    
    //Label for qs
    @IBOutlet weak var label: UILabel!
    
    //button for choices
    @IBOutlet weak var Action: UIButton!
    
    
    @IBAction func Action(_ sender: AnyObject)
    {
        if (sender.tag == Int(rightAnswerPlacement))
        {
            print("Right")
            
        }
        else
        {
            print("Wrong")
            let alert = UIAlertController(title: "That is incorrect", message: "Try again!", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: nil))
            present(alert, animated: true)
            currentQuestion -= 1
            //how to record which wrong answer the user is pressing
            //let WrongAnswer = sender.tag.titleLabel?.text
            

        }

This is a code for a basic quiz app on Xcode, what I am trying to do with my first few lines of code is trying to make an option disappear once I click another button


回答1:


Reason for the error is that both your button and a function are named Action. In the line that gives you error, compiler thinks you are referring to the function (which is of type (AnyObject) -> ()) instead of the button. Rename either your function, or your button.

By the way, there are couple of things you should improve in your code:

  • by convention, names of types are uppercased, while names of instances should be lowercased
  • use more descriptive names for your variables and functions - i.e. hintButton instead of Hint, actionButton instead of Action for UIButton and actionButtonTapped for func


来源:https://stackoverflow.com/questions/63581325/i-am-not-sure-why-i-keep-getting-the-error-value-of-type-anyobject-has

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!