2015-09-04 72 views
1

我正在做一個地圖應用程序,並且遇到了一個問題。Coredata:未將Coredata獲取結果投射到對象

我似乎無法讓Coredata將結果轉換爲相關的Pin數據。

這裏是我使用從CD

func fetchAllPins() -> [Pin] { 
    let error: NSErrorPointer = nil 

    // Create the Fetch Request 
    let fetchRequest = NSFetchRequest(entityName: "Pin") 
    fetchRequest.returnsObjectsAsFaults = false 

    // Execute the Fetch Request 
    let results = sharedContext.executeFetchRequest(fetchRequest, error: error) 

    // Check for Errors 
    if error != nil { 
     println("Error in fetchAllEvents(): \(error)") 
    } 
    println("RESULTS ARE") 
    println(results) // Gives results (see console below) 

    return results as! [Pin] // Returns Blank no idea why 

} 

下載銷的方法,下面是從控制檯輸出顯示數據被返回。

Optional([<Pin: 0x7fb9144dcf60> (entity: Pin; id: 0xd000000000040000 <x-coredata://5E886C33-FB0F-4035-9362-489A0BBFB0D9/Pin/p1> ; data: { 
    lat = "54.98870385960889"; 
    long = "-2.732679843506143"; 
    reference = 1; 
}), <Pin: 0x7fb911697ee0> (entity: Pin; id: 0xd000000000080000 <x-coredata://5E886C33-FB0F-4035-9362-489A0BBFB0D9/Pin/p2> ; data: { 
    lat = "54.58505971892202"; 
    long = "-1.719484882196165"; 
    reference = 1; 
})]) 

這裏是我現在用的是腳物件:

import Foundation 
import MapKit 
import CoreData 

@objc(Pin) 

class Pin : NSManagedObject { 
    struct Keys { 
     static let lat = "lat" 
     static let long = "long" 
    } 
    @NSManaged var lat:Double 
    @NSManaged var long: Double 
    @NSManaged var reference: NSNumber 




override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?) { 
     super.init(entity: entity, insertIntoManagedObjectContext: context) 
     println("-----") 

     println(entity) 
    } 

init(llat: CLLocationDegrees, llong: CLLocationDegrees, llinksToImages: [String]?, context: NSManagedObjectContext) { 

    let entity = NSEntityDescription.entityForName("Pin", inManagedObjectContext: context)! 
    super.init(entity: entity,insertIntoManagedObjectContext: context) 
    lat = llat as Double 
    long = llong as Double 
    reference = 1 

} 

任何意見將非常歡迎

回答

0

我設法破解它..將是可愛理解爲什麼我的解決辦法了有沒有工作,但萬一有人需要它:

如果更換:

return results as! [Pin] 

而是將它推入一個自己的解壓縮結果數組中,它似乎很好玩。例如:

var myAwesomeResults = [Pin]() 
for pinItem in results{ 
myAwesomeResults.append(pinItem) 
} 
return myAwesomeResults // returns data correctly!