forked from jessesquires/JSQCoreDataKit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoreDataEntityProtocol.swift
More file actions
57 lines (47 loc) · 1.68 KB
/
Copy pathCoreDataEntityProtocol.swift
File metadata and controls
57 lines (47 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// Created by Jesse Squires
// http://www.jessesquires.com
//
//
// Documentation
// http://www.jessesquires.com/JSQCoreDataKit
//
//
// GitHub
// https://github.com/jessesquires/JSQCoreDataKit
//
//
// License
// Copyright © 2015 Jesse Squires
// Released under an MIT license: http://opensource.org/licenses/MIT
//
import CoreData
import Foundation
/// Describes an entity in Core Data.
public protocol CoreDataEntityProtocol: class {
/// The name of the entity.
static var entityName: String { get }
/// The default sort descriptors for a fetch request.
static var defaultSortDescriptors: [NSSortDescriptor] { get }
}
public extension CoreDataEntityProtocol where Self: NSManagedObject {
/// Returns a default entity name for this managed object based on its class name.
public static var entityName: String {
return "\(Self.self)"
}
/// Returns a new fetch request with `defaultSortDescriptors`.
public static var fetchRequest: NSFetchRequest<Self> {
let request = NSFetchRequest<NSManagedObject>(entityName: entityName)
request.sortDescriptors = defaultSortDescriptors
return request as! NSFetchRequest<Self>
}
/// Returns the entity with the specified name from the managed object model associated
/// with the specified managed object context’s persistent store coordinator.
///
/// - parameter context: The managed object context to use.
///
/// - returns: Returns the entity description for this managed object.
public static func entity(context: NSManagedObjectContext) -> NSEntityDescription {
return NSEntityDescription.entity(forEntityName: entityName, in: context)!
}
}