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
}
任何意見將非常歡迎