swiftuitextfieldcrash-log

Thread 0 crashed


I submitted my app to app store, they said it's crash when type url & press "Done" from keyboard, But my real device, my tester friends devices and simulator we tested on 13.3.1 it's working fine! Not able to find whats the issues here.

this crash log from apple developer :

enter image description here

my code:

 //MARK:-- TEXTFIELD DELEGATE
  func textFieldShouldReturn(_ textField: UITextField) -> Bool {

      let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
      let matches = detector.matches(in: textField.text!, options: [], range: NSRange(location: 0, length: textField.text!.utf16.count))
      
      for match in matches {
          guard let range = Range(match.range, in: textField.text!) else { continue }
          let url = textField.text![range]
          print("return: " + url)
      
          let Turl = String(url)
           DispatchQueue.main.async {
                     self.tableView.reloadData()
                 }
      if Turl.isEmpty {
          self.name.becomeFirstResponder()
          return false
      }else{
        
        self.url.becomeFirstResponder()
        
        UserDefaults.standard.set(Turl, forKey: "URL")
        
        DispatchQueue.main.async {
                   self.tableView.reloadData()
               }
    
        textField.resignFirstResponder()

        dismiss(animated: true){
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
        }

        
        //apped data to array and save to userDefault
        var saved = URLArray as! [String]
        var named = nameArray
        let PlaylistName = name.text!
          
        if !saved.contains("\(textField.text!)"){
              
            saved.append(Turl)
            named.append(PlaylistName)
            UserDefaults.standard.set(saved, forKey: "URLs")
            UserDefaults.standard.set(named, forKey: "name")
            print(named, saved)
            
            self.tableView.reloadData()
              
            
        }else{
            print("was saved")
            
            dismiss(animated: true, completion: nil)
          }
      }
  }
    
     if textField == name{
        self.name.resignFirstResponder()
        self.url.becomeFirstResponder()
      }
    
      self.tableView.reloadData()
      
      return true

  }

Solution

  • Exception has Exception Code = 1 (second line in your exception):

    EXC_BREAKPOINT (SIGTRAP)
    Exception Codes: 0x0000000000000001
    

    which is usually the result of force unwrapping values that are nil.

    For instance try! and textField.text!, or any other force unwrap you have may cause it. Instead, use guards and return from function (possibly with error) if they fail. E.g:

        guard let detector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) else {
            // Do something if Failed
            return;
        }
    

    Also it will be much easier for you to debug if you include symbol files with your build.