Guest User

Untitled

a guest
Jan 24th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. extension TestPeriods {
  2.  
  3. @nonobjc public class func fetchRequest() -> NSFetchRequest<TestPeriods> {
  4. return NSFetchRequest<TestPeriods>(entityName: "TestPeriods")
  5. }
  6.  
  7. @NSManaged public var periods: [PeriodClass]?
  8.  
  9. }
  10.  
  11. public class PeriodClass: NSObject, NSCoding {
  12.  
  13.  
  14. var start: Date?
  15. var end: Date?
  16. var period: Int16?
  17.  
  18. public func encode(with aCoder: NSCoder) {
  19. aCoder.encode(start, forKey: "start")
  20. aCoder.encode(end, forKey: "end")
  21. aCoder.encode(period, forKey: "period")
  22. }
  23.  
  24.  
  25. public required init?(coder aDecoder: NSCoder) {
  26. start = aDecoder.decodeObject(forKey: "start") as! Date
  27. end = aDecoder.decodeObject(forKey: "end") as! Date
  28. period = aDecoder.decodeObject(forKey: "period") as! Int16
  29. }
  30.  
  31.  
  32. init(start: Date, end: Date, period: Int16) {
  33. super.init()
  34. self.start = start
  35. self.end = end
  36. self.period = period
  37. }
  38.  
  39. }
  40.  
  41. let container = TestPeriods(context: PersistenceServce.context)
  42. // The 'period' I refer to below is just an object with the same start, end, and period and I know is not nil
  43. let x = PeriodClass(start: period.start!, end: period.end!, period: period.period)
  44. container.periods?.append(x)
  45.  
  46. // Saving the object here
  47. PersistenceServce.saveContext()
  48. // I know this Persistence Service is not a problem as I use it for other Core Data Objects
  49.  
  50. let tFetch: NSFetchRequest<TestPeriods> = TestPeriods.fetchRequest()
  51. do {
  52. let classes = try PersistenceServce.context.fetch(tFetch)
  53. print("TEST CLASS COUNT (classes.count)")
  54. ...
  55. }
  56.  
  57. print("TEST CLASS PERIODS (classes[0].periods)")
  58.  
  59. container.periods?.append(x)
  60.  
  61. container.periods = [x]
Add Comment
Please, Sign In to add comment