I am struggling with the following case in Swift :
I have a NSManaged class called Event :
import UIKit
import Foundation
import CoreData
class Event: NSManagedObject, NSCoding {
@NSManaged var eventArchived: Bool
@NSManaged var eventCCRecipientAddress: String?
@NSManaged var eventCCRecipientName: String?
@NSManaged var eventEndDate: String?
@NSManaged var eventEndRSVPDateTime: String?
@NSManaged var eventHappeningId: String?
@NSManaged var eventName: String?
@NSManaged var eventResourcesFolderName: String?
@NSManaged var eventStartDate: String?
@NSManaged var eventId: String?
@NSManaged var eventHappening: Happening
override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?) {
super.init(entity: entity, insertIntoManagedObjectContext: context)
}
// MARK: NSCoding protocol conformance
required init(coder aDecoder: NSCoder) {
let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext!
let entity = NSEntityDescription.entityForName("Event", inManagedObjectContext: context)!
// Note: pass `nil` to `insertIntoManagedObjectContext`
super.init(entity: entity, insertIntoManagedObjectContext: nil)
if let eventArchived = aDecoder.decodeObjectForKey("eventArchived") as? Bool {
self.eventArchived = eventArchived
}
if let eventCCRecipientAddress = aDecoder.decodeObjectForKey("eventCCRecipientAddress") as? String {
self.eventCCRecipientAddress = eventCCRecipientAddress
}
if let eventCCRecipientName = aDecoder.decodeObjectForKey("eventCCRecipientName") as? String {
self.eventCCRecipientName = eventCCRecipientName
}
if let eventEndDate = aDecoder.decodeObjectForKey("eventEndDate") as? String {
self.eventEndDate = eventEndDate
}
if let eventEndRSVPDateTime = aDecoder.decodeObjectForKey("eventEndRSVPDateTime") as? String {
self.eventEndRSVPDateTime = eventEndRSVPDateTime
}
if let eventHappeningId = aDecoder.decodeObjectForKey("eventHappeningId") as? String {
self.eventHappeningId = eventHappeningId
}
if let eventName = aDecoder.decodeObjectForKey("eventName") as? String {
self.eventName = eventName
}
if let eventResourcesFolderName = aDecoder.decodeObjectForKey("eventResourcesFolderName") as? String {
self.eventResourcesFolderName = eventResourcesFolderName
}
if let eventStartDate = aDecoder.decodeObjectForKey("eventStartDate") as? String {
self.eventStartDate = eventStartDate
}
if let eventId = aDecoder.decodeObjectForKey("eventId") as? String {
self.eventId = eventId
}
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(eventArchived, forKey: "eventArchived")
if let eventCCRecipientAddress = self.eventCCRecipientAddress {
aCoder.encodeObject(eventCCRecipientAddress, forKey: "eventCCRecipientAddress")
}
if let eventCCRecipientName = self.eventCCRecipientName {
aCoder.encodeObject(eventCCRecipientName, forKey: "eventCCRecipientName")
}
if let eventEndDate = self.eventEndDate {
aCoder.encodeObject(eventEndDate, forKey: "eventEndDate")
}
if let eventEndRSVPDateTime = self.eventEndRSVPDateTime {
aCoder.encodeObject(eventEndRSVPDateTime, forKey: "eventEndRSVPDateTime")
}
if let eventCCRecipientAddress = self.eventCCRecipientAddress {
aCoder.encodeObject(eventCCRecipientAddress, forKey: "eventCCRecipientAddress")
}
if let eventHappeningId = self.eventHappeningId {
aCoder.encodeObject(eventHappeningId, forKey: "eventHappeningId")
}
if let eventName = self.eventName {
aCoder.encodeObject(eventName, forKey: "eventName")
}
if let eventResourcesFolderName = self.eventResourcesFolderName {
aCoder.encodeObject(eventResourcesFolderName, forKey: "eventResourcesFolderName")
}
if let eventStartDate = self.eventStartDate {
aCoder.encodeObject(eventStartDate, forKey: "eventStartDate")
}
if let eventId = self.eventId {
aCoder.encodeObject(eventId, forKey: "eventId")
}
aCoder.encodeObject(eventHappening, forKey: "eventHappening")
}
}
The class used to be "simple" with only the NSManaged variables until I've been asked to implement the NSCoding protocol. I use NSCoding because I need to be aware of the Current Event anywhere in my app, including offline mode.
When I try to use my Event Class, I keep having the following error :
CoreData: error: Failed to call designated initializer on NSManagedObject class 'AccueilInviteVIP.Event'
This is how I try to access my class :
var currentEvent = Event()
Then in viewDidLoad Method :
// currentEvent
currentEvent = NSEntityDescription.insertNewObjectForEntityForName("Event", inManagedObjectContext: self.managedObjectContext!) as! Event
And it crashes on this line.
The error should be pretty clear, it seems that my init methods are incorrect but I cannot get it working, as I am new to Swift and can't find any example close to this.
Any help would be great !
Thank you,
Hugo