【问题标题】:Swift: Use of instance member on type 'ViewController'; did you mean to use a value of type 'ViewController' insteadSwift:在“ViewController”类型上使用实例成员;你的意思是改用“ViewController”类型的值吗
【发布时间】:2017-01-18 23:30:06
【问题描述】:

正如标题所示,Swift 告诉我我正在调用 ViewController 类的成员,而不是对象。我查看了许多其他帖子,但无法弄清楚我哪里出错了。

func endEventSteps() {
    UserDefaults.standard.set(false, forKey: "hasLoggedIn")
    UserDefaults.standard.set(nil, forKey: "usersEmail")
    UserDefaults.standard.set(nil, forKey: "usersPassword"
    performSegue(withIdentifier: "backToLoginScreen", sender: self)
}
let alertController = UIAlertController(title: "End event", message: "Are you sure you'd like to end this event?", preferredStyle: UIAlertControllerStyle.alert)
let DestructiveAction = UIAlertAction(title: "End",
                                          style: UIAlertActionStyle.destructive,
                                          handler: {(alert: UIAlertAction!) in endEventSteps()})

编辑:我在最后一行收到以下错误:“在类型 'InfoSubmisionViewController' 上使用实例成员 'endEventSteps';您的意思是使用类型为 'InfoSubmisionViewController' 的值吗?”。当我将简单的命令作为处理程序时,代码编译得很好,例如 print("foo")

以上所有代码都在自定义 ViewController 类“InfoSubmissionViewController”中。

【问题讨论】:

  • 你的错误在哪一行?我确实注意到 UserDefaults.standard.set(nil, forKey: "usersPassword" 缺少右括号。
  • 这可能只是一个复制粘贴问题。错误在最后一行,我将 endEventSteps() 设置为处理程序。 swift 告诉我:在类型“InfoSubmisionViewController”上使用实例成员“endEventSteps”;您的意思是改用“InfoSubmisionViewController”类型的值吗?
  • 你在哪里调用 alertController?
  • @NateMann 在按钮的 IBAction 中
  • 该代码具有误导性,因为它假装在类的顶层执行,这会导致(不同的)编译器错误。

标签: ios xcode swift3


【解决方案1】:

假设你有一个名为DestructiveAction的控制器类,那么

let DestructiveAction = ...

应该是

let destructiveAction = ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 2021-06-29
    • 2017-04-23
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多