2015-10-04 85 views
0

這是我的代碼。它循環查找數據庫中的數字記錄,但僅檢索第一個記錄lat和lon。MapKit從核心數據中添加多個註釋

func fetch() { 
    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
    let context: NSManagedObjectContext = appDel.managedObjectContext! 
    let freq = NSFetchRequest(entityName: "Mappoints") 
    let fetchResults = try! context.executeFetchRequest(freq) as! [NSManagedObject] 
    self.mapView.delegate = self 
    myData = fetchResults 
    myData.count 
    for _ in myData { 
     let data: NSManagedObject = myData[row] 

    lat = (data.valueForKey("latitude") as? String)! 
    lon = (data.valueForKey("longitude") as? String)! 

    let latNumb = (lat as NSString).doubleValue 
    let longNumb = (lon as NSString).doubleValue 
    let signLocation = CLLocationCoordinate2DMake(latNumb, longNumb) 
    addAnnotaion(signLocation) 
    } 

} 

我確定我缺少一些簡單的東西,但只是不斷丟失它。

回答

0

這做的是我結束瞭解決問題的代碼。

func fetch() { 
    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
    let context: NSManagedObjectContext = appDel.managedObjectContext! 
    let freq = NSFetchRequest(entityName: "Mappoints") 
    let fetchResults = try! context.executeFetchRequest(freq) as! [NSManagedObject] 
    self.mapView.delegate = self 
    myData = fetchResults 
    let ct = myData.count // Add this line 
    // Then changed the for statement from for _ in myData 
    // To the line below and now all map points show up. 
    for row in 0...ct-1 { 
     let data: NSManagedObject = myData[row] 
     lat = (data.valueForKey("latitude") as? String)! 
     lon = (data.valueForKey("longitude") as? String)! 
     let latNumb = (lat as NSString).doubleValue 
     let longNumb = (lon as NSString).doubleValue 
     let signLocation = CLLocationCoordinate2DMake(latNumb, longNumb) 
     addAnnotaion(signLocation) 
    } 
1

你的循環看起來很奇怪。你說myData[row],但你似乎沒有增加行。如果該行不增加,則data變量將始終相同。

例如,您可以for data in myData { ...