【问题标题】:Swift Compiler Error : Command failed due to signal: Segmentation fault: 11Swift 编译器错误:由于信号导致命令失败:分段错误:11
【发布时间】:2017-03-14 21:23:01
【问题描述】:

我是 Xcode 和 swift 的新手,但我已经能够将错误源缩小到这行代码。我已经搜索了所有地方,但找不到解决方案。我目前正在使用 Xcode 8.1。

        button.addTarget( self, action: #selector(handleRegister), for:.touchUpInside)

这是我的一些代码

import UIKit

class LoginController: UIViewController {

let backGroundImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.image = UIImage(named: "backgrd_image")
    return imageView
}()

let inputsContainerView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor.white
    view.translatesAutoresizingMaskIntoConstraints = false
    view.layer.cornerRadius = 5
    view.layer.masksToBounds = true
    return view

}()

    let loginRegisterButton: UIButton = {
    let button = UIButton(type: .system)
    button.backgroundColor = UIColor(r: 80, g: 101, b: 161)
    button.setTitle("Register", for: .normal)
    button.setTitleColor(UIColor.white, for: .normal)
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
    button.translatesAutoresizingMaskIntoConstraints = false

    button.addTarget( self, action: #selector(handleRegister), for:.touchUpInside)
    return button
}()

// when register button is clicked, printout 123
func handleRegister() {
    print(123)

}

有谁知道是什么导致了这个分段错误:11 错误。

【问题讨论】:

  • 分段错误不是编译器错误,而是运行时错误。
  • 不一定。随着时间的推移,快速编译器在尝试编译我的代码时崩溃了很多。运行时崩溃在 iOS 中生成 EXC_INV_OP,而不是分段错误。分段错误存在于 MacOS 中。
  • @e.iluf 您需要转到“构建”日志并粘贴与崩溃对应的日志条目。这适用于 Xcode 5,但足够接近:stackoverflow.com/questions/19014359/…

标签: swift xcode


【解决方案1】:

要使用 self,您必须在第一行使用“lazy var”而不是“let”,如下所示。在函数内部,self 引用函数“loginRegisterButton”,使用“lazy var”,self 将引用您的控制器

lazy var loginRegisterButton: UIButton = {
  let button = UIButton(type: .system)
  button.backgroundColor = UIColor(r: 80, g: 101, b: 161)
  button.setTitle("Register", for: .normal)
  button.setTitleColor(UIColor.white, for: .normal)
  button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
  button.translatesAutoresizingMaskIntoConstraints = false

  button.addTarget( self, action: #selector(handleRegister), for:.touchUpInside)
return button
}()

【讨论】:

    【解决方案2】:

    删除这一行:

     button.addTarget( self, action: #selector(handleRegister), for:.touchUpInside)
    

    并写在viewDidLoad函数中

    【讨论】:

      【解决方案3】:

      恕我直言 原因是您在“button.addTarget(self, action:#selector(handleRegister), for:.touchUpInside)”中使用了“self”。检查您是否可以插入“nil”而不是“self”。在您尝试插入“nil”之后,您可以添加“print(self)”以了解“self”不是您想要的“LoginController”。

      【讨论】:

      • 成功了!..你是我的英雄,我希望一切顺利。谢谢!
      • 感谢@Andrey 很好的回答!
      猜你喜欢
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 2015-12-15
      • 2017-06-23
      • 2018-11-06
      • 1970-01-01
      相关资源
      最近更新 更多