我在Swift中遇到以下情况:

我有一个称为事件的NSManaged类:

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")
}
}


在要求我实现NSCoding协议之前,该类曾经是仅带有NSManaged变量的“简单”类。我使用NSCoding是因为我需要在应用程序中的任何地方(包括离线模式)都了解当前事件。

当我尝试使用事件类时,始终出现以下错误:

CoreData: error: Failed to call designated initializer on NSManagedObject class 'AccueilInviteVIP.Event'


这就是我尝试访问班级的方式:

var currentEvent = Event()


然后在viewDidLoad方法中:

    // currentEvent
    currentEvent = NSEntityDescription.insertNewObjectForEntityForName("Event", inManagedObjectContext: self.managedObjectContext!) as! Event


它在此行崩溃。

该错误应该很清楚,似乎我的init方法不正确,但是我无法使其正常工作,因为我是Swift的新手,找不到与之接近的任何示例。

任何帮助都会很棒!

谢谢,

雨果

最佳答案

看起来工程过度,对我来说太多了。

为什么不只存储eventId并从Core Data中检索对象?还要存档,它看起来确实很凌乱和多余。

// store
NSUserDefaults.standardUserDefaults().setObject(event.eventId forKey:"mainEventID")

// retrieve
let eventId = NSUserDefaults.standardUserDefaults().stringForKey("mainEventId")!
let request = NSFetchRequest(entityName: "Event")
request.predicate = NSPredicate(format: "eventId = %@", eventId)
let event = (context.executeFetchRequest(request, error:nil) as [Event]).first!


您将不得不执行更多的错误和nil检查,但是您明白了。

关于ios - Swift-核心数据和NSCoding:无法调用指定的初始化程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31511934/

10-09 01:25