EXC_BAD_INSTRUCTION

Hello everyone!

I have a problem, when I run my app in iOS Simulator and I get the following error, but when I run the same code in my iPhone, I don't get the error. (I call a function closeTaskService in line 42)


Please help me!!

I use Xcode 8; and the error is: "Thread 1: EXC_BAD_INSTRUCTION (code=EXC_l386_INVOP, subcode=0x0)"; I get a error in lines 34 and 83 ....


import UIKit
import Charts
class HomeViewController: UIViewController {
    
    
    @IBOutlet weak var pieChart: PieChartView!
    @IBOutlet weak var barChart: BarChartView!
    @IBOutlet weak var menuButton: UIBarButtonItem!
    
    @IBOutlet weak var lblContado: UILabel!
    @IBOutlet weak var lblCredito: UILabel!
    
    let dato = NSUserDefaults()
    
    let typeCredit = ["Crédito", "Contado"]
    
    
    private var monto:[Double] = []
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        UINavigationBar.appearance().tintColor = UIColor.whiteColor()
        UINavigationBar.appearance().barStyle = .Black
        UINavigationBar.appearance().backgroundColor = UIColor(red: 61.0/255.0, green: 205.0/255.0, blue: 135.0/255.0, alpha:1)
        
        if self.revealViewController() != nil {
            menuButton.target = self.revealViewController()
            menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }
        
        self.closeTaskService("1", completionHandler: { (response) -> () in
            print(response)
        })
        self.closeTaskService("2", completionHandler: { (response) -> () in
            print(response)
        })
        
        / 
        let cont = self.dato.objectForKey("Contado")
        let monCon = cont as AnyObject? as? String
        let valCont:String = (monCon as AnyObject? as? String)!
        print("monto contado: "+String(valCont))
        let conValue = atoi(String(valCont))
        
        / 
        let cred = self.dato.objectForKey("Credito")
        let monCred = cred as AnyObject? as? String
        let valCred:String = (monCred as AnyObject? as? String)!
        print("monto credito: "+String(valCred))
        let credValue = atoi(String(valCred))
        
        / 
        self.lblContado.text = "¢"+valCont
        self.lblContado.textColor = UIColor.whiteColor()
        self.lblContado.font = lblContado.font.fontWithSize(16)
        self.lblContado.textAlignment = NSTextAlignment.Right
        
        
        self.lblCredito.text = "¢"+valCred
        self.lblCredito.textColor = UIColor.whiteColor()
        self.lblCredito.font = lblContado.font.fontWithSize(16)
        self.lblCredito.textAlignment = NSTextAlignment.Right
        
        / 
        setChart(typeCredit, values: [conValue,credValue])
    }
    
    func atoi (valor: String)->Double{
        / 
        let value = (valor as NSString).doubleValue
        / 
        return value
    }
    
    func closeTaskService(tipo: String, completionHandler: (response:NSString) -> ()) {
        let fecha = getCurrentDay()
        let request = NSMutableURLRequest(URL: NSURL(string: "http:/ 
        let db = self.dato.objectForKey("dbName")
        let db1 = db as AnyObject? as? String
        let dbName : String = (db1 as AnyObject? as? String)!
        print(dbName)
        request.HTTPMethod = "POST"
        let postParams = "fecha="+fecha+"&"+"DB_name="+dbName+"&"+"tipo="+tipo
        print(postParams)
        request.HTTPBody = postParams.dataUsingEncoding(NSUTF8StringEncoding)
        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
            data, response, error in
            / 
            let responseString: NSString = NSString(data: data!, encoding: NSUTF8StringEncoding)!
            / 
            switch tipo{
                
                case "1":
                    print("Entre al 1")
                    self.dato.setValue(responseString as String, forKey: "Contado")
                    print(self.dato.objectForKey("Contado"))
                case "2":
                    print("Entre al 2")
                    self.dato.setValue(responseString as String, forKey: "Credito")
                    print(self.dato.objectForKey("Credito"))
                default:
                    self.dato.setValue("0", forKey: "Contado")
                    self.dato.setValue("0", forKey: "Credito")
            }
        }
        task.resume()
    }
    
    func getCurrentDay ()->String{
        let todaysDate:NSDate = NSDate()
        let dateFormatter:NSDateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy/MM/dd"
        let todayString:String = dateFormatter.stringFromDate(todaysDate)
        / 
        return todayString
    }
    
    func setChart(dataPoints: [String], values: [Double]){
        var dataEntries: [ChartDataEntry] = []
        
        for i in 0..<dataPoints.count {
            let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
            dataEntries.append(dataEntry)
        }
        
        let pieChartDataSet = PieChartDataSet(yVals: dataEntries, label: "")
        let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)
        pieChart.data = pieChartData
        
        var colors: [UIColor] = []
        
        for _ in 0..<dataPoints.count {
            let red = Double(arc4random_uniform(256))
            let green = Double(arc4random_uniform(256))
            let blue = Double(arc4random_uniform(256))
            
            let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
            colors.append(color)
        }
        pieChartDataSet.colors = colors
    }
    
}

In the context of Swift code an

EXC_BAD_INSTRUCTION
machine exception usually means that you’ve tripped over one of Swift’s safety checks. I posted some background to this on another thread.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
EXC_BAD_INSTRUCTION
 
 
Q