Hi
I tried to follow the idea here: https://gist.github.com/capttaco/adb38e0d37fbaf9c004eto make my CoreData code more type safe. So I added this protocol to my project:
protocol Fetchable // Fetchable from https://gist.github.com/capttaco/adb38e0d37fbaf9c004e
{
typealias FetchableType: NSManagedObject
static func entityName() -> String
static func objectsInContext(context: NSManagedObjectContext, predicate: NSPredicate?, sortedBy: String?, ascending: Bool) throws -> [FetchableType]
static func oneObjectInContext(context: NSManagedObjectContext, predicate: NSPredicate?, sortedBy: String?, ascending: Bool) throws -> FetchableType?
static func allObjectsInContext(context: NSManagedObjectContext, sortedBy: String?, ascending: Bool) throws -> [FetchableType]
static func objectCountInContext(context: NSManagedObjectContext, predicate: NSPredicate?) -> Int
static func fetchRequest(context: NSManagedObjectContext, predicate: NSPredicate?, sortedBy: String?, ascending: Bool) -> NSFetchRequest
}
Then this protocol extension:
extension Fetchable where Self : NSManagedObject, FetchableType == Self
{
static func objectsInContext(context:NSManagedObjectContext,predicate:NSPredicate? = nil,sortedBy:String? = nil,ascending:Bool = false) throws -> [FetchableType] {
let request = fetchRequest(context, predicate: predicate, sortedBy: sortedBy, ascending: ascending)
let fetchResults = try context.executeFetchRequest(request)
return fetchResults as! [FetchableType]
}
static func oneObjectInContext(context:NSManagedObjectContext,predicate:NSPredicate? = nil,sortedBy:String? = nil,ascending:Bool = false) throws -> FetchableType? {
let managedObjects: [FetchableType] = try objectsInContext(context, predicate: predicate, sortedBy: sortedBy, ascending: ascending)
guard managedObjects.count > 0 else { return nil }
return managedObjects.first
}
static func allObjectsInContext(context: NSManagedObjectContext, sortedBy: String? = nil, ascending: Bool = false) throws -> [FetchableType] {
return try objectsInContext(context, sortedBy: sortedBy, ascending: ascending)
}
static func objectCountInContext(context: NSManagedObjectContext, predicate: NSPredicate? = nil) -> Int {
let request = fetchRequest(context, predicate: predicate)
let error: NSErrorPointer = nil
let count = context.countForFetchRequest(request, error: error)
guard error != nil else {
NSLog("Error retrieving data %@, %@", error, error.debugDescription)
return 0;
}
return count;
}
static func fetchRequest(context: NSManagedObjectContext, predicate: NSPredicate? = nil, sortedBy: String? = nil, ascending: Bool = false) -> NSFetchRequest {
let request = NSFetchRequest()
let entity = NSEntityDescription.entityForName(entityName(), inManagedObjectContext: context)
request.entity = entity
request.predicate = predicate // Can be nil
if sortedBy != nil {
let sort = NSSortDescriptor(key: sortedBy, ascending: ascending)
request.sortDescriptors = [sort]
}
return request
}
}
Then I added this protocol to one of my NSManagedObject classes:
extension LooseOffering: Fetchable { ///@@@ This is giving Does not conform to protocol error
class func entityName() -> String {
return "LooseOffering"
}
}
But as the comment says, this makes xcode give me a little red triangle saying "Type LooseOffering does not conform to protocol Fetchable.
LooseOffering is in Objective-C:
@class Account, Count, Event;
@interface LooseOffering : NSManagedObject
@property (nonatomic, retain) Account *account;
@property (nonatomic, retain) Count *count;
@property (nonatomic, retain) Event *event;
@end
Can anyone see what is happening there? This is in XCode 7.0 beta 5.
TIA
Mark