我正在做一个地图应用程序,我遇到了一个问题。
我似乎无法让Coredata将结果转换为相关的Pin数据。
以下是我用来从CD下载Pins的方法
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;
})])
这是我正在使用的Pin对象:
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 :(得分:0)
我设法破解了..很高兴理解为什么我的解决方案没有用,但万一有人需要它:
如果替换:
return results as! [Pin]
而是把它推入一个自己的解压缩结果数组,它似乎很好玩。 例如:
var myAwesomeResults = [Pin]()
for pinItem in results{
myAwesomeResults.append(pinItem)
}
return myAwesomeResults // returns data correctly!