无法创建映射策略 – iOS Core Data

我一直在尝试在一个应用程序中使用自定义迁移策略.到目前为止,迁移已经从v1 – > v2时使用映射模型.但是,无论何时向实体映射添加自定义策略,迁移都将拒绝为v2 – > V3.

自定义迁移政策:

import Foundation
import CoreData

class ObjectCustomV2V3Migration: NSEntityMigrationPolicy {

override func createDestinationInstancesForSourceInstance(sInstance: NSManagedobject,entityMapping mapping: NSEntityMapping,manager: NSMigrationManager,error: NSErrorPointer) -> Bool {

    var newObject:NSManagedobject? = NSEntityDescription.insertNewObjectForEntityForName(mapping.destinationEntityName!,inManagedobjectContext: manager.destinationContext) as? NSManagedobject

    // Sets attr31 - string attribute
    var str:String = sInstance.valueForKey("attr21") as String
    if str == "" {
        str = "BlanketyBlank"
    }
    newObject?.setValue(str,forKey: "attr31")

    // Sets attr32 (int16 attribute) as double of value in prevIoUs version
    // ignore the dodgy type casting.
    var num:Int = sInstance.valueForKey("attr22") as Int
    num *= 2
    var num16 = NSNumber(integer: num)

    newObject?.setValue(num16,forKey: "attr32")



    if newObject != nil {
        manager.associateSourceInstance(sInstance,withDestinationInstance: newObject!,forEntityMapping: mapping)
        return true
    }


    return false


   }
}

当我运行应用程序时,会返回以下错误

2015-07-10 14:32:42.952 SingleDBMigration[2153:739674] CoreData: error: -addPersistentStoreWithType:sqlite configuration:(null) URL:file:///var/mobile/Containers/Data/Application/6C142EC2-02DB-4BD6-8428-5739C57C7795/Documents/SingleDBMigration.sqlite options:{
    NSInferMappingModelAutomaticallyOption = 0;
    NSMigratePersistentStoresAutomaticallyOption = 1;
} ... returned error Error Domain=NSCocoaErrorDomain Code=134110 "The operation Couldn’t be completed. (Cocoa error 134110.)" UserInfo=0x17ecad70 {NSUnderlyingException=Couldn't create mapping policy for class named (ObjectCustomV2V3Migration)} with userInfo dictionary {
    NSUnderlyingException = "Couldn't create mapping policy for class named (ObjectCustomV2V3Migration)";
}
2015-07-10 14:32:42.965 SingleDBMigration[2153:739674] Unresolved error Optional(Error Domain=YOUR_ERROR_DOMAIN Code=9999 "Failed to initialize the application's saved data" UserInfo=0x17eaf880 {NSLocalizedDescription=Failed to initialize the application's saved data,NSUnderlyingError=0x17ecad90 "The operation Couldn’t be completed. (Cocoa error 134110.)",NSLocalizedFailureReason=There was an error creating or loading the application's saved data.}),Optional([NSLocalizedDescription: Failed to initialize the application's saved data,NSUnderlyingError: Error Domain=NSCocoaErrorDomain Code=134110 "The operation Couldn’t be completed. (Cocoa error 134110.)" UserInfo=0x17ecad70 {NSUnderlyingException=Couldn't create mapping policy for class named (ObjectCustomV2V3Migration)},NSLocalizedFailureReason: There was an error creating or loading the application's saved data.])

重要的部分是:

NSUnderlyingException=Couldn't create mapping policy for class named (ObjectCustomV2V3Migration)

我已经尝试了关于这个问题的几个问题,他们都没有提供任何令人满意的解决方案.如果任何人在我面对的问题上有所了解,我将非常感激!

谢谢!

解决方法

在理解类似问题的答案之后,我终于解决了这个问题.我忘了添加项目命名空间,所以不要在映射模型中将自定义策略名称设置为ObjectCustomV2V3Migration,我应该使用ProjectModuleName.ObjectCustomV2V3Migration.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...