Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension TestPeriods {
- @nonobjc public class func fetchRequest() -> NSFetchRequest<TestPeriods> {
- return NSFetchRequest<TestPeriods>(entityName: "TestPeriods")
- }
- @NSManaged public var periods: [PeriodClass]?
- }
- public class PeriodClass: NSObject, NSCoding {
- var start: Date?
- var end: Date?
- var period: Int16?
- public func encode(with aCoder: NSCoder) {
- aCoder.encode(start, forKey: "start")
- aCoder.encode(end, forKey: "end")
- aCoder.encode(period, forKey: "period")
- }
- public required init?(coder aDecoder: NSCoder) {
- start = aDecoder.decodeObject(forKey: "start") as! Date
- end = aDecoder.decodeObject(forKey: "end") as! Date
- period = aDecoder.decodeObject(forKey: "period") as! Int16
- }
- init(start: Date, end: Date, period: Int16) {
- super.init()
- self.start = start
- self.end = end
- self.period = period
- }
- }
- let container = TestPeriods(context: PersistenceServce.context)
- // The 'period' I refer to below is just an object with the same start, end, and period and I know is not nil
- let x = PeriodClass(start: period.start!, end: period.end!, period: period.period)
- container.periods?.append(x)
- // Saving the object here
- PersistenceServce.saveContext()
- // I know this Persistence Service is not a problem as I use it for other Core Data Objects
- let tFetch: NSFetchRequest<TestPeriods> = TestPeriods.fetchRequest()
- do {
- let classes = try PersistenceServce.context.fetch(tFetch)
- print("TEST CLASS COUNT (classes.count)")
- ...
- }
- print("TEST CLASS PERIODS (classes[0].periods)")
- container.periods?.append(x)
- container.periods = [x]
Add Comment
Please, Sign In to add comment