Reputation: 13
I have some problem about executeFetchRequest
import UIKit
import CoreData
class BeaconDB: NSObject {
var addStatus: BeaconAddStatus!
enum BeaconAddStatus{
case DUPLICATE_IN_AD
case ADDED_SUCCESSFULL
case ERROR_IN_ADD
}
func addNewBeacon(beacon: BeaconData) -> BeaconAddStatus{
print("ADDNewBeacon")
print("uuid: %@ \(beacon.uuid)")
let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext = appDelegate.managedObjectContext
let entityDesc: NSEntityDescription = NSEntityDescription.entityForName("Beacon", inManagedObjectContext: context)!
let request: NSFetchRequest = NSFetchRequest()
let predSearch: NSPredicate = NSPredicate(format: "(uuid = %@) AND (major = %@) AND (minor = %@) \(beacon.uuid), \(beacon.major), \(beacon.minor)")
request.entity = entityDesc
request.predicate = predSearch
do {
let existingBeacon: Beacons = try context.executeFetchRequest(request)
// success ...
} catch let error as NSError {
// failure
print("Fetch failed: \(error.localizedDescription)")
}
}
}
Mv Beacons class is NSManagedObject
import UIKit
import CoreData
class Beacons: NSManagedObject {
dynamic var major : NSNumber = 0.0
dynamic var minor : NSNumber = 0.0
dynamic var name : NSString = ""
dynamic var uuid : NSString = ""
}
But it's error from this line
let existingBeacon: Beacons = try context.executeFetchRequest(request)
Cannot convert value of type ['Any Object'] to specified type 'Beacons'
Help me please Thank you :))
Upvotes: 1
Views: 2444
Reputation: 285150
Two issues:
executeFetchRequest
returns always an array of objects.let existingBeacons = try context.executeFetchRequest(request) as! [Beacons]
Cannot convert value of type ... to specified type ... means usually the compiler needs help by casting the type or there is no relation at all.
PS: It's recommended to name Core Data entities in the singular form to avoid confusion.
Upvotes: 3