我不知道为什么我不断收到类型“(AnyObject) ->"的错误值()' 没有成员 'currentTitle' [英] I am not sure why I keep getting the error Value of type '(AnyObject) -> ()' has no member 'currentTitle'
问题描述
@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
}
这是 Xcode 上的基本测验应用程序的代码,我尝试使用前几行代码尝试在单击另一个按钮后使选项消失
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
推荐答案
错误的原因是你的按钮和函数都被命名为 Action
.在给您错误的行中,编译器认为您指的是函数(类型为 (AnyObject) -> ()
)而不是按钮.重命名您的函数或按钮.
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:
- 按照惯例,类型名称大写,而实例名称应小写
- 为变量和函数使用更具描述性的名称 - 即
hintButton
代替Hint
,actionButton
代替Action
> 用于UIButton
和actionButtonTapped
用于func
- 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 ofHint
,actionButton
instead ofAction
forUIButton
andactionButtonTapped
forfunc
这篇关于我不知道为什么我不断收到类型“(AnyObject) ->"的错误值()' 没有成员 'currentTitle'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!