3

I am new to Xcode and swift but I have been able to narrow the error source down to this line of code. I have searched every where but could not find the solution. I'm currently using Xcode 8.1.

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

this is some of my code

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)

}

does anyone know what could be causing this Segmentation fault: 11 error.

e.iluf
  • 1,389
  • 5
  • 27
  • 69
  • Segmentation fault is not a compiler error, it's a runtime error. – Alexander Nov 01 '16 at 17:28
  • 1
    Not necessarily. The swift compiler has crashed a lot trying to compile my code over time. Runtime crashes generate EXC_INV_OP in iOS, not segmentation faults. Segmentation faults are in MacOS. – BaseZen Nov 01 '16 at 17:41
  • @e.iluf You need to go to the "Build" log and paste the log entry that corresponds to the crash. This is for Xcode 5 but it's close enough: http://stackoverflow.com/questions/19014359/how-do-i-view-the-full-build-log-on-xcode5 – BaseZen Nov 01 '16 at 17:42

3 Answers3

2

IMHO the reason is that you use "self" in "button.addTarget( self, action: #selector(handleRegister), for:.touchUpInside)". To check that you could insert "nil" instead of "self". And further after you try inserting "nil", you could add "print(self)" to understand that "self" is referring not to "LoginController" as you intended.

Andrey
  • 489
  • 6
  • 14
0

Delete this line:

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

and write it in the viewDidLoad function

dparoli
  • 8,891
  • 1
  • 30
  • 38
0

To use self, you will have to use "lazy var" instead of "let" on the first line like below. Within the function, self refers to the function "loginRegisterButton", using "lazy var", self will refer to your controller

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
}()
mut tony
  • 357
  • 7
  • 9