Thread 1: EXC_BAD_INSTRUCTION (code=1, subcode=0x0)

i'm trying to use MarqueeLabel @IBOutlet weak var liveLabel: MarqueeLabel!

override func viewDidLoad() {
    super.viewDidLoad()
    self.liveLabelSetUp()
}
func liveLabelSetUp() {
    // Continuous, with tap to pause
    liveLabel.type = .continuousReverse //that's the line of the error
    liveLabel.speed = .duration(10)
    liveLabel.fadeLength = 10.0
    liveLabel.leadingBuffer = 40.0
    liveLabel.trailingBuffer = 30.0
    liveLabel.isUserInteractionEnabled = true
    let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.pauseTap))
    tapRecognizer.numberOfTapsRequired = 1
    tapRecognizer.numberOfTouchesRequired = 1
    liveLabel.addGestureRecognizer(tapRecognizer)
  }
   
  @objc func pauseTap(_ recognizer: UIGestureRecognizer) {
    let continuousLabel2 = recognizer.view as! MarqueeLabel
    if recognizer.state == .ended {
      continuousLabel2.isPaused ? continuousLabel2.unpauseLabel() : continuousLabel2.pauseLabel()
    }
  }

Error code : Thread 1: EXC_BAD_INSTRUCTION (code=1, subcode=0x0)

Answered by OOPer in 691892022

If you find the crash on the line liveLabel.type = .continuousReverse, the most likely reason would be that the @IBOutlet is not properly connected and the property liveLabel is nil at runtime.

Have you checked all the possibility that liveLabel might not be connected properly?

Error code : Thread 1: EXC_BAD_INSTRUCTION (code=1, subcode=0x0)

  • Please show complete code of the class where you use liveLabel
  • Where does it crash exactly ?
  • Show the complete crash log.
    func requestLiveInfo(){
    print("requestLiveInfo")
    let url = URL(string:"https://release.hihealth.com.tw/api/MoveV/live-info")
    var req = URLRequest(url: url!)
    req.httpMethod = "GET"
    let task = URLSession.shared.dataTask(with: req) { data, response, error in
      guard let data = data, error == nil else {
        print("request fail")
        print(error?.localizedDescription ?? "No data")
        DispatchQueue.main.async {
          showAlert(target: self, title: "Error", msg: "Please confirm your internet connection status", cancelString: nil, confirmString: "reorganize", callback: { (btnindex) in
             
            if btnindex == 1{
              print("Please confirm your internet connection status")
              self.requestLiveInfo()
            }
             
          });
        }
        return
      }
      if let httpResponse = response as? HTTPURLResponse {
        print("request success")
        print(httpResponse.statusCode)
         
        if (httpResponse.statusCode == 200){
           
          do{
            let responseString = String(bytes: data, encoding: String.Encoding.utf8)
            print(responseString)
            if let responseJSON = try JSONSerialization.jsonObject(with: data, options: []) as? [String:String]{
              let dateFormatter = DateFormatter()
              dateFormatter.timeZone = TimeZone(identifier: "Asia/Taipei")
              dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
              if let startTimeString = responseJSON["started_at"],
                let endTimeString = responseJSON["ended_at"],
                let nameString = responseJSON["name"]{
                if let start = dateFormatter.date(from: startTimeString),
                  let end = dateFormatter.date(from: endTimeString){
                  let now = Date()
                  if(now >= start && now <= end){
                    DispatchQueue.main.async {
                      self.liveBellImg.image = UIImage(named: "live-bell-on")
                      self.liveButtonImg.image = UIImage(named: "live-join-on")
                      self.liveLabelSetUp()   //Use here
                      self.liveLabel.text = "Live:\(nameString)"
                    }
                  }
                  else{
                    DispatchQueue.main.async {
                      dateFormatter.dateFormat = "MM/dd HH:mm"
                      self.liveBellImg.image = UIImage(named: "live-bell-off")
                      self.liveButtonImg.image = UIImage(named: "live-join-off")
                      self.liveLabelSetUp()   //Use here
                      self.liveLabel.text = "\(dateFormatter.string(from: start)) Live:\(nameString)"
                    }
                  }
                  DispatchQueue.main.async {
                    let uiTap = UITapGestureRecognizer(target: self, action: #selector(self.goToLive))
                    self.liveButtonImg.addGestureRecognizer(uiTap)
                  }
                }
                else{
                  DispatchQueue.main.async {
                    showAlert(target: self, title: "Error", msg: "Format is wrong", cancelString: nil, confirmString: "confirm", callback: { (btnindex) in
                      if btnindex == 1{
                      }
                    });
                  }
                }
              }
              else{
                DispatchQueue.main.async {
                  showAlert(target: self, title: "Error", msg: "Live information data is missing", cancelString: nil, confirmString: "confirm", callback: { (btnindex) in
                    if btnindex == 1{
                    }
                  });
                }
              }
            }
          }
          catch{
            DispatchQueue.main.async {
              showAlert(target: self, title: "Error", msg: "The live broadcast information data format is incorrect", cancelString: nil, confirmString: "confirm", callback: { (btnindex) in
                if btnindex == 1{
                }
              });
            }
          }
        }
        else
        {
          DispatchQueue.main.async {
            let responseString = String(bytes: data, encoding: String.Encoding.utf8)
            showAlert(target: self, title: "Error", msg: responseString ?? "Unknown", cancelString: nil, confirmString: "confirm", callback: { (btnindex) in
              if btnindex == 1{
              }
            });
          }
        }
      }
    }
    task.resume()
  }

crash exactly

 func liveLabelSetUp() {
    // Continuous, with tap to pause
    liveLabel.type = .continuousReverse  //crash
    liveLabel.speed = .duration(10)
    liveLabel.fadeLength = 10.0
    liveLabel.leadingBuffer = 40.0
    liveLabel.trailingBuffer = 30.0
    liveLabel.isUserInteractionEnabled = true
    let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.pauseTap))
    tapRecognizer.numberOfTapsRequired = 1
    tapRecognizer.numberOfTouchesRequired = 1
    liveLabel.addGestureRecognizer(tapRecognizer)
  }
Accepted Answer

If you find the crash on the line liveLabel.type = .continuousReverse, the most likely reason would be that the @IBOutlet is not properly connected and the property liveLabel is nil at runtime.

Have you checked all the possibility that liveLabel might not be connected properly?

If IBOutlet not connected, connect it.

If connected, connection may be damaged:

  • remove the connection (click on the small x in front of IBOutlet in IB)
  • reconnect
  • do a Clean Build Folder to be completely sure everything OK.
Thread 1: EXC_BAD_INSTRUCTION (code=1, subcode=0x0)
 
 
Q