线程 1:致命错误:索引超出范围。没有快速从数组中获取值,控制台显示它们不是空数组
Posted
技术标签:
【中文标题】线程 1:致命错误:索引超出范围。没有快速从数组中获取值,控制台显示它们不是空数组【英文标题】:Thread 1: Fatal error: Index out of range. Not getting a value from array in swift, console shows they're not empty arrays 【发布时间】:2018-05-14 17:18:31 【问题描述】:我得到一个:
线程 1:致命错误:索引超出范围
当我调用一个函数时。
我有一个函数应该在存储到 2 个 CLLocation2D 数组中的值之间进行比较。第一个,trackingRouteArray
,通过监视用户位置获取附加的 CLLocation2D。第二个,alertNotificationArray
,由存储 CLLocation2D 的双击附加。
我有两个计数器 trackingCoordinatesArrayPosition
和 alertNotificationArrayPosition
用于递增数组位置以从中获取值。
现在我的问题是trackingRouteArray
在我引用它的位置时有一个值,但alertNotificationArray
没有。
在控制台中,我打印了两个数组,它们都填充了值。
代码如下:
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
我在let alertLatitude = alertNotificationArray[alertNotificationArrayPosition].latitude
line 上收到错误消息
这些是来自控制台的打印:
跟踪停止 警报的坐标[__ C.CLLocationCoordinate2D(纬度:37.333775436275886,经度:-122.02368602396592),__C.CLLocationCoordinate2D(纬度:37.332980802736785,经度:-122.02355882939459),__C.CLLocationCoordinate2D(纬度:37.333891009356435,经度:-122.0235224889466),__C.CLLocationCoordinate2D(纬度:37.33415106952107,经度:-122.02355883066487),__C.CLLocationCoordinate2D(纬度:37.334338890591638,经度:-122.02332261163552),__C.CLLocationCoordinate2D(纬度:37.334598949139817,经度:-122.02328627075829),__C.CLLocationCoordinate2D(纬度:37.335696975135136,经度:-122.0236315182422 )] 路线的坐标[__ C.CLLocationCoordinate2D(纬度:37.333153109999998,经度:-122.02360022000001),__C.CLLocationCoordinate2D(纬度:37.333184799999998,经度:-122.02360543),__C.CLLocationCoordinate2D(纬度:37.333217210000001,经度:-122.02360829),__C.CLLocationCoordinate2D(纬度:37.333247679999999,经度:-122.02360903),__C.CLLocationCoordinate2D(纬度:37.333278159999999,经度:-122.02360912),__C.CLLocationCoordinate2D(纬度:37.333308330000001,经度:-122.02360862),__C.CLLocationCoordinate2D(纬度:37.333339840000001,经度:-122.02360667000001 ),__C.CLLocationCoordinate2D(纬度:37.333372369999999,经度:-122.02360458),__C.CLLocationCoordinate2D(纬度:37.333404880000003,经度:-122.02360226),__C.CLLocationCoordinate2D(纬度:37.333437250000003,经度:-122.02360114),__C.CLLocationCoordinate2D(纬度: 37.333469319999999,经度:-122.02360106),__C.CLLocationCoordinate2D(纬度:37.33 3500979999997,经度:-122.0236012),__C.CLLocationCoordinate2D(纬度:37.333533869999997,经度:-122.02360059999999),__C.CLLocationCoordinate2D(纬度:37.333567180000003,经度:-122.02360006000001),__C.CLLocationCoordinate2D(纬度:37.333599149999998,经度:-122.02359964999999), __C.CLLocationCoordinate2D(纬度:37.333630999999997,经度:-122.0236005),__C.CLLocationCoordinate2D(纬度:37.333663510000001,经度:-122.02360109),__C.CLLocationCoordinate2D(纬度:37.33369742,经度:-122.02360057999999),__C.CLLocationCoordinate2D(纬度:37.33373237,经度:-122.02359865),__C.CLLocationCoordinate2D(纬度:37.333768640000002,经度:-122.0235964),__C.CLLocationCoordinate2D(纬度:37.333807470000004,经度:-122.02359255),__C.CLLocationCoordinate2D(纬度:37.333850239999997,经度:-122.02359082),__C。 CLLocationCoordinate2D(纬度:37.333894520000001,经度:-122.02358845000001),__C.CLLocationCoordin ate2D(纬度:37.333937650000003,经度:-122.02358579),__C.CLLocationCoordinate2D(纬度:37.333980240000002,经度:-122.02358187999999),__C.CLLocationCoordinate2D(纬度:37.334027380000002,经度:-122.02357784),__C.CLLocationCoordinate2D(纬度:37.334073750000002,经度: -122.02357465),__C.CLLocationCoordinate2D(纬度:37.334120370000001,经度:-122.0235716),__C.CLLocationCoordinate2D(纬度:37.334169469999999,经度:-122.02356758000001),__C.CLLocationCoordinate2D(纬度:37.334220510000002,经度:-122.02356580999999),__C.CLLocationCoordinate2D(纬度:37.334273039999999,经度:-122.02356597000001),__C.CLLocationCoordinate2D(纬度:37.334326740000002,经度:-122.02356743999999),__C.CLLocationCoordinate2D(纬度:37.334379980000001,经度:-122.02356723),__C.CLLocationCoordinate2D(纬度:37.334436080000003,经度:-122.02356829 ), __C.CLLocationCoordinate2D(纬度: 37.334504440000003, 经度: -122.02357289),__C.CLLocationCoordinate2D(纬度:37.334565410000003,经度:-122.02357538),__C.CLLocationCoordinate2D(纬度:37.334625559999999,经度:-122.02357759),__C.CLLocationCoordinate2D(纬度:37.334685870000001,经度:-122.02357768),__C.CLLocationCoordinate2D(纬度:37.334747299999997,经度:-122.02357773),__C.CLLocationCoordinate2D(纬度:37.33480909,经度:-122.02357759),__C.CLLocationCoordinate2D(纬度:37.334872420000004,经度:-122.02357723),__C.CLLocationCoordinate2D(纬度:37.334942730000002,经度:-122.02357637 ),__C.CLLocationCoordinate2D(纬度:37.335018179999999,经度:-122.023573),__C.CLLocationCoordinate2D(纬度:37.335095870000004,经度:-122.02356672000001),__C.CLLocationCoordinate2D(纬度:37.335175319999998,经度:-122.02355701),__C.CLLocationCoordinate2D(纬度: 37.335259520000001,经度:-122.02354482),__C.CLLocationCoordinate2D(纬度:37.335346260000001,经度UDE:-122.02352918),__C.CLLocationCoordinate2D(纬度:37.335435189999998,经度:-122.02351114),__C.CLLocationCoordinate2D(纬度:37.335526700000003,经度:-122.02348929),__C.CLLocationCoordinate2D(纬度:37.335615359999998,经度:-122.02346115),__C。 CLLocationCoordinate2D(纬度:37.335705070000003,经度:-122.02342901999999),__C.CLLocationCoordinate2D(纬度:37.335796440000003,经度:-122.02339327),__C.CLLocationCoordinate2D(纬度:37.33589113,经度:-122.02335769),__C.CLLocationCoordinate2D(纬度:37.33598464,经度: -122.02332491999999),__C.CLLocationCoordinate2D(纬度:37.336079929999997,经度:-122.02329487999999),__C.CLLocationCoordinate2D(纬度:37.336179360000003,经度:-122.02327117),__C.CLLocationCoordinate2D(纬度:37.336281159999999,经度:-122.02325363999999),__C.CLLocationCoordinate2D(纬度:37.3363838,经度:-122.02324118999999),__C.CLLocationCoordinate2D(纬度:37.3364818 80000001,经度:-122.02323437),__c.cllocationCoord2d(纬度:37.336578070000002,经度:-122.02323232000001),__C.cllocationCoord2D(纬度:37.33667375999999996,经度:-122.02322977999999)] (lldb)`
和
自 fix_it_mapView.MapViewController 0x00007fb37980b000 trackingLatitude CLLocationDegrees 37.333153109999998 trackingLongitude CLLocationDegrees -122.02360022000001 alertLatitude CLLocationDegrees alertLongitude CLLocationDegrees调用checkAlerts()
的函数会在调用它之前打印数组,但似乎调用之前已执行,我只是无法确定它是否是拼写错误。
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()
【问题讨论】:
【参考方案1】:下面的代码可能是问题所在,因为它允许alertNotificationArrayPosition
上升到等于alertNotificationArray.count
,这意味着索引超出范围错误,
else if alertNotificationArrayPosition < alertNotificationArray.count
alertNotificationArrayPosition = alertNotificationArrayPosition + 1
我也认为 while 条件在您使用 !=
而不是 <
或 <=
时看起来很奇怪
【讨论】:
是的.. 这就是问题所在,改为else if alertNotificationArrayPosition < ((alertNotificationArray.count) - 1) alertNotificationArrayPosition = alertNotificationArrayPosition + 1
我忘记了if else
语句中的-1。 != 不是问题,我只是用它来避免拼写错误,因为 >= 而不是 didEnterRegion 的区域类型
我使用distance(from: coordinate)
方法解决了检查部分,并检查了该距离是否
以上是关于线程 1:致命错误:索引超出范围。没有快速从数组中获取值,控制台显示它们不是空数组的主要内容,如果未能解决你的问题,请参考以下文章
SwiftUI - 致命错误:从数组中删除元素时索引超出范围