マッピングポリシーを作成できませんでした - iOS Core Data 質問する

マッピングポリシーを作成できませんでした - 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)

この問題に関して見つけたいくつかの質問を試してみましたが、どれも満足のいく解決策を提供しませんでした。私が直面している問題について、どなたかが説明していただければ大変ありがたく思います。

ありがとう!

ベストアンサー1

同様の質問に対する回答を理解した後、最終的にこれを解決しました。プロジェクトの名前空間を追加するのを忘れていたので、ObjectCustomV2V3Migrationマッピング モデルのようにカスタム ポリシー名を設定するのではなく、 を使用する必要がありましたProjectModuleName.ObjectCustomV2V3Migration

おすすめ記事