-1

I am getting

"EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)"

in this code

else {
    let latitude = NSString(string: places[activePlace]["lat"]!).doubleValue
    let longitude = NSString(string: places[activePlace]["lon"]!).doubleValue
    let coordinate = CLLocationCoordinate2DMake(latitude, longitude)
    let latDelta:CLLocationDegrees = 0.01
    let lonDelta:CLLocationDegrees = 0.01
    let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
    let region:MKCoordinateRegion = MKCoordinateRegionMake(coordinate, span)
    self.Map.setRegion(region, animated: true)
    let annotation = MKPointAnnotation()
    annotation.coordinate = coordinate

    annotation.title = places[activePlace]["name"]
    self.Map.addAnnotation(annotation)

on

 let latitude = NSString(string: places[activePlace]["lat"]!).doubleValue

here is my active place variable witch is in a previous table view

var activePlace = -1]

still in the tableview I have this

 override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {

        activePlace = indexPath.row

        return indexPath

    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "newPlace" {

            activePlace = -1


        }

in the second view controller ( where I have the problem ) I have this in the view did load

manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest

if activePlace == -1 {

    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()


} else {



    let latitude = NSString(string: places[activePlace]["lat"]!).doubleValue

    let longitude = NSString(string: places[activePlace]["lon"]!).doubleValue

    let coordinate = CLLocationCoordinate2DMake(latitude, longitude)

    let latDelta:CLLocationDegrees = 0.01

    let lonDelta:CLLocationDegrees = 0.01

    let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)

    let region:MKCoordinateRegion = MKCoordinateRegionMake(coordinate, span)

    self.Map.setRegion(region, animated: true)

    let annotation = MKPointAnnotation()

    annotation.coordinate = coordinate


    annotation.title = places[activePlace]["name"]

    self.Map.addAnnotation(annotation)

This code was already there and worked perfect until I added the following code

@IBAction func addCurentLoc(sender: UIBarButtonItem) {

        var newCoordinate2 = self.Map.userLocation.location!.coordinate;

        var location = CLLocation(latitude: newCoordinate2.latitude, longitude: newCoordinate2.longitude)

        title = "new address"


        let annotation = MKPointAnnotation();
        annotation.title = title;
        annotation.coordinate = self.Map.userLocation.location!.coordinate;

        CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) -> Void in

            var title = ""

            if (error == nil) {

                if let p = placemarks?[0] {

                    var subThouroughfare:String = ""
                    var thouroughfare:String = ""

                    if p.subThoroughfare != nil {

                        subThouroughfare = p.subThoroughfare!

                    }

                    if p.thoroughfare != nil {

                        thouroughfare = p.thoroughfare!

                    }

                    title = "\(subThouroughfare) \(thouroughfare)"


                }

            }

            if title == "" {

                title = "Added \(NSDate())"

            }

            places.append(["name":title,"lat":"\(newCoordinate2.latitude)","lon":"\(newCoordinate2.longitude)"])

            let annotation = MKPointAnnotation()

            annotation.coordinate = newCoordinate2

            annotation.title = title

            self.Map.addAnnotation(annotation)

        }

        self.Map.addAnnotation(annotation);



        func mapView(mapView: MKMapView!,
            viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!{
                if(annotation is MKUserLocation){
                    return nil;
                }

                let pinView: Void = mapView.addAnnotation(annotation);
                let pinAnnotationView = MKPinAnnotationView(annotation: annotation,reuseIdentifier:"MyIdentifier");
                return pinAnnotationView;

                       }



    }

I have been looking for an a solution for a while and can't find out, note that I have checked this answer

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) on dispatch_semaphore_dispose

as well as this one

what does Error "Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)" mean?

but haven't found the solution, thanks for the help

EDIT: Please note that this question has been modified to be a bit clearer, please ask if you need more info.

Community
  • 1
  • 1
stephen D
  • 255
  • 1
  • 5
  • 16
  • Put a breakpoints and you can find at which line you are getting this error. – Dharmesh Kheni Dec 09 '15 at 09:12
  • 1
    put the expression print(places[activePlace]["lat"]) before your first assignment (as first statement after else { ) and show us the result – user3441734 Dec 09 '15 at 09:30
  • @user3441734 the result is exactly the same except the error message is on the print(places[activePlace]["lat"]) line – stephen D Dec 09 '15 at 10:17
  • it seem that your array places is declared as implicitly unwrap optional and is still nil while you try to access it. from the code you provide in your question, we can not see, what exactly you did. – user3441734 Dec 09 '15 at 10:35

1 Answers1

0

Put a breakpoint on the first line of your else then type in lldb:

po places[activePlace] - does it return value or nil?

if returns value type places[activePlace]["lat"]!

One of them will probably return nil, the error you're getting means you're implicitly unwrapping a nil.

66o
  • 756
  • 5
  • 10
  • How do you type something in the LLDB ? – stephen D Dec 09 '15 at 10:48
  • 1) Run the app 2) Open bottom pane if not already open 3) on the left side you have all the variables in current scope, on the right there are your log messages and that's where you type your commands once you hit a breakpoint – 66o Dec 09 '15 at 14:41
  • 1
    this is the message I get po places[activePlace] fatal error: Array index out of range error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0). The process has been returned to the state before expression evaluation. (lldb) – stephen D Dec 09 '15 at 17:13
  • well the compiler message is quite clear here. You want to access an element that doesn't exist in an array. Can not help you any more with the code you posted. Read your code again, check where you set activePlace variable and work from there – 66o Dec 10 '15 at 14:54
  • thanks for answering @66o I have been looking through the code but haven't figured it out , however I have added code in the question with should help – stephen D Dec 14 '15 at 08:38