0

I get a:

Thread 1: Fatal error: Index out of range

when I call a function.

I have a function that should perform a comparison between values stored into 2 CLLocation2D arrays. The first,trackingRouteArray, gets CLLocation2D appended by monitoring user position. The second, alertNotificationArray, gets appended by double taps that store the CLLocation2D.

I have two counters trackingCoordinatesArrayPositionand alertNotificationArrayPosition that are used to increment array position to get values from.

Now my problem is that the trackingRouteArray has a value when I refer to its positions, but alertNotificationArraydoesn't. In the console I print both arrays and they are both populated with values.

Here's the code:

func checkAlerts() {
    while trackingCoordinatesArrayPosition != ( (trackingCoordinatesArray.count) - 1) {
        let trackingLatitude = trackingCoordinatesArray[trackingCoordinatesArrayPosition].latitude
        let trackingLongitude = trackingCoordinatesArray[trackingCoordinatesArrayPosition].longitude
        let alertLatitude = alertNotificationArray[alertNotificationArrayPosition].latitude
        let alertLongitude = alertNotificationArray[alertNotificationArrayPosition].longitude

        if trackingLatitude == alertLatitude && trackingLongitude == alertLongitude {
            // do something
            print( "found problem")
        }
        else if alertNotificationArrayPosition < alertNotificationArray.count {
            alertNotificationArrayPosition = alertNotificationArrayPosition + 1
        }
        else if  alertNotificationArrayPosition == alertNotificationArray.count {
            trackingCoordinatesArrayPosition = ( trackingCoordinatesArrayPosition + 1)
            alertNotificationArrayPosition = 0
        }
    }
}

I get the error on the let alertLatitude = alertNotificationArray[alertNotificationArrayPosition].latitudeline

These are the prints from console:

tracking stopped
alerts coordinates are[__C.CLLocationCoordinate2D(latitude: 37.333775436275886, longitude: -122.02368602396592), __C.CLLocationCoordinate2D(latitude: 37.332980802736785, longitude: -122.02355882939459), __C.CLLocationCoordinate2D(latitude: 37.333891009356435, longitude: -122.0235224889466), __C.CLLocationCoordinate2D(latitude: 37.33415106952107, longitude: -122.02355883066487), __C.CLLocationCoordinate2D(latitude: 37.334338890591638, longitude: -122.02332261163552), __C.CLLocationCoordinate2D(latitude: 37.334598949139817, longitude: -122.02328627075829), __C.CLLocationCoordinate2D(latitude: 37.335696975135136, longitude: -122.0236315182422)] route coordinates are[__C.CLLocationCoordinate2D(latitude: 37.333153109999998, longitude: -122.02360022000001), __C.CLLocationCoordinate2D(latitude: 37.333184799999998, longitude: -122.02360543), __C.CLLocationCoordinate2D(latitude: 37.333217210000001, longitude: -122.02360829), __C.CLLocationCoordinate2D(latitude: 37.333247679999999, longitude: -122.02360903), __C.CLLocationCoordinate2D(latitude: 37.333278159999999, longitude: -122.02360912), __C.CLLocationCoordinate2D(latitude: 37.333308330000001, longitude: -122.02360862), __C.CLLocationCoordinate2D(latitude: 37.333339840000001, longitude: -122.02360667000001), __C.CLLocationCoordinate2D(latitude: 37.333372369999999, longitude: -122.02360458), __C.CLLocationCoordinate2D(latitude: 37.333404880000003, longitude: -122.02360226), __C.CLLocationCoordinate2D(latitude: 37.333437250000003, longitude: -122.02360114), __C.CLLocationCoordinate2D(latitude: 37.333469319999999, longitude: -122.02360106), __C.CLLocationCoordinate2D(latitude: 37.333500979999997, longitude: -122.0236012), __C.CLLocationCoordinate2D(latitude: 37.333533869999997, longitude: -122.02360059999999), __C.CLLocationCoordinate2D(latitude: 37.333567180000003, longitude: -122.02360006000001), __C.CLLocationCoordinate2D(latitude: 37.333599149999998, longitude: -122.02359964999999), __C.CLLocationCoordinate2D(latitude: 37.333630999999997, longitude: -122.0236005), __C.CLLocationCoordinate2D(latitude: 37.333663510000001, longitude: -122.02360109), __C.CLLocationCoordinate2D(latitude: 37.33369742, longitude: -122.02360057999999), __C.CLLocationCoordinate2D(latitude: 37.33373237, longitude: -122.02359865), __C.CLLocationCoordinate2D(latitude: 37.333768640000002, longitude: -122.0235964), __C.CLLocationCoordinate2D(latitude: 37.333807470000004, longitude: -122.02359255), __C.CLLocationCoordinate2D(latitude: 37.333850239999997, longitude: -122.02359082), __C.CLLocationCoordinate2D(latitude: 37.333894520000001, longitude: -122.02358845000001), __C.CLLocationCoordinate2D(latitude: 37.333937650000003, longitude: -122.02358579), __C.CLLocationCoordinate2D(latitude: 37.333980240000002, longitude: -122.02358187999999), __C.CLLocationCoordinate2D(latitude: 37.334027380000002, longitude: -122.02357784), __C.CLLocationCoordinate2D(latitude: 37.334073750000002, longitude: -122.02357465), __C.CLLocationCoordinate2D(latitude: 37.334120370000001, longitude: -122.0235716), __C.CLLocationCoordinate2D(latitude: 37.334169469999999, longitude: -122.02356758000001), __C.CLLocationCoordinate2D(latitude: 37.334220510000002, longitude: -122.02356580999999), __C.CLLocationCoordinate2D(latitude: 37.334273039999999, longitude: -122.02356597000001), __C.CLLocationCoordinate2D(latitude: 37.334326740000002, longitude: -122.02356743999999), __C.CLLocationCoordinate2D(latitude: 37.334379980000001, longitude: -122.02356723), __C.CLLocationCoordinate2D(latitude: 37.334436080000003, longitude: -122.02356829), __C.CLLocationCoordinate2D(latitude: 37.334504440000003, longitude: -122.02357289), __C.CLLocationCoordinate2D(latitude: 37.334565410000003, longitude: -122.02357538), __C.CLLocationCoordinate2D(latitude: 37.334625559999999, longitude: -122.02357759), __C.CLLocationCoordinate2D(latitude: 37.334685870000001, longitude: -122.02357768), __C.CLLocationCoordinate2D(latitude: 37.334747299999997, longitude: -122.02357773), __C.CLLocationCoordinate2D(latitude: 37.33480909, longitude: -122.02357759), __C.CLLocationCoordinate2D(latitude: 37.334872420000004, longitude: -122.02357723), __C.CLLocationCoordinate2D(latitude: 37.334942730000002, longitude: -122.02357637), __C.CLLocationCoordinate2D(latitude: 37.335018179999999, longitude: -122.023573), __C.CLLocationCoordinate2D(latitude: 37.335095870000004, longitude: -122.02356672000001), __C.CLLocationCoordinate2D(latitude: 37.335175319999998, longitude: -122.02355701), __C.CLLocationCoordinate2D(latitude: 37.335259520000001, longitude: -122.02354482), __C.CLLocationCoordinate2D(latitude: 37.335346260000001, longitude: -122.02352918), __C.CLLocationCoordinate2D(latitude: 37.335435189999998, longitude: -122.02351114), __C.CLLocationCoordinate2D(latitude: 37.335526700000003, longitude: -122.02348929), __C.CLLocationCoordinate2D(latitude: 37.335615359999998, longitude: -122.02346115), __C.CLLocationCoordinate2D(latitude: 37.335705070000003, longitude: -122.02342901999999), __C.CLLocationCoordinate2D(latitude: 37.335796440000003, longitude: -122.02339327), __C.CLLocationCoordinate2D(latitude: 37.33589113, longitude: -122.02335769), __C.CLLocationCoordinate2D(latitude: 37.33598464, longitude: -122.02332491999999), __C.CLLocationCoordinate2D(latitude: 37.336079929999997, longitude: -122.02329487999999), __C.CLLocationCoordinate2D(latitude: 37.336179360000003, longitude: -122.02327117), __C.CLLocationCoordinate2D(latitude: 37.336281159999999, longitude: -122.02325363999999), __C.CLLocationCoordinate2D(latitude: 37.3363838, longitude: -122.02324118999999), __C.CLLocationCoordinate2D(latitude: 37.336481880000001, longitude: -122.02323437), __C.CLLocationCoordinate2D(latitude: 37.336578070000002, longitude: -122.02323232000001), __C.CLLocationCoordinate2D(latitude: 37.336673759999996, longitude: -122.02322977999999)]
(lldb)`

and

 
self    fix_it_mapView.MapViewController    0x00007fb37980b000
trackingLatitude    CLLocationDegrees   37.333153109999998
trackingLongitude   CLLocationDegrees   -122.02360022000001
alertLatitude   CLLocationDegrees   
alertLongitude  CLLocationDegrees

The function from where checkAlerts() gets called does the printing of the arrays before calling it, but it seems that the call get performed before and I just can't find out whether it's a typo or else.

func stopTracking() {
    locationManager.stopUpdatingLocation()
    let duration = daysLasted(start: dateStart, end: Date())
    print("tracking stopped")
    self.dataReceived = "bici rubata"

    self.endTrackingPoint.coordinate = (locationManager.location?.coordinate)!  // gets courrent user location and store it into end tracking point
    self.trackingRoute.append(self.endTrackingPoint)                            // append end poit to route array
    mapView.showAnnotations(trackingRoute, animated: true)  // shows start and end ponits only
    drawRoutePolyline()  //draw route
    print("alerts coordinates are\(alertNotificationArray)")
    print("route coordinates are\(trackingCoordinatesArray)")
    checkAlerts()
} 
Abizern
  • 146,289
  • 39
  • 203
  • 257
Vincenzo
  • 5,304
  • 5
  • 38
  • 96

1 Answers1

0

The below code is probably the issue since it allows alertNotificationArrayPosition to go up to be equal to alertNotificationArray.count which will mean an index out of range error,

 else if alertNotificationArrayPosition < alertNotificationArray.count {
    alertNotificationArrayPosition = alertNotificationArrayPosition + 1
 }

I also think the while condition looks very strange where you are using != rather than < or <=

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • yes.. that was the problem, changed to `else if alertNotificationArrayPosition < ((alertNotificationArray.count) - 1) { alertNotificationArrayPosition = alertNotificationArrayPosition + 1 } ` I forgot the -1 in the `if else` statements. The != is not a problem, I just use it to avoid typos as >= instead of <= and vice versa, but a <= would do just the same. Now I need to set a range for alertNotificationArray values so they don't have do be identical, sort of regions for `didEnterRegion` – Vincenzo May 14 '18 at 18:07
  • I resolved the checking part using the `distance(from: coordinate)` method, and checkin if that distance would be <= 10 meters. that would be the same that setting a circle of 10 meters radius and check id the user coordinates are within the circle boundaries. looks promising.. a workaround to implement the same userDidEnterRegion functionality, but without the restriction of 20 regions. as alway thank you very much. – Vincenzo May 15 '18 at 10:08