我是Swift語言的新手。我創建了一個MapKit應用程序,它從Sqlite DB(最新FMDB堆棧)遞歸檢索MKPointAnnotation
數據(經緯度,日誌和標題)。Swift錯誤:致命錯誤:無法索引空緩衝區
目的是在MKMapViewDelegate
上放置一堆興趣點。我已經嘗試了w/out數組,但是mapView.addAnnotation
覆蓋了任何點並僅顯示了地圖上的最後一個點,所以我嘗試使用數組。
我已經創建了一個函數,但是當調用wpoint數組時,我在運行時遇到了錯誤「致命錯誤:無法索引空緩衝區」。
下面的代碼:
func initializeRoute()
{
sharedInstance.database!.open()
var resultSet: FMResultSet! = sharedInstance.database!.executeQuery("SELECT * FROM Route", withArgumentsInArray: nil)
// DB Structure
var DBorder: String = "order" // Int and Primary Index
var DBlatitude: String = "latitude" // Float
var DBlongitude: String = "longitude" // Float
// Array declaration
var wpoint: [MKPointAnnotation] = []
// Loop counter init
var counter: Int = 0
if (resultSet != nil) {
while resultSet.next() {
counter = counter + 1
wpoint[counter].coordinate = CLLocationCoordinate2DMake(
(resultSet.stringForColumn(String(DBlatitude)) as NSString).doubleValue,
(resultSet.stringForColumn(String(DBlongitude)) as NSString).doubleValue
)
wpoint[counter].title = resultSet.stringForColumn(DBorder)
mapView.addAnnotation(wpoint[counter])
}
}
sharedInstance.database!.close()
}
println ("Coordinate = \(wpoint.coordinate)")
顯示所有的數據,我搞亂數組聲明中的一些信息
謝謝Anna,我已經解決了重新聲明Array:'var wpoint:Array = Array()',現在它可以工作! :) –
2015-01-10 16:49:31
我已經實施了你的建議,非常感謝! – 2015-01-10 17:03:22