What is the select syntax for custom Core Data policies?

I am trying to implement very basic migration using Core Data. The only property was originally created as Integer 16 with values ​​0 or 1. In the new version of the model, this property was changed to Boolean , and the migration policy below should handle it. I saw several examples written in Swift and they don't seem to make access open / open or add @objc to make it accessible to Objective-C. I did this to eliminate any reason why it does not work.

I created a user policy mapping model to map an entity to the following expression.

FUNCTION($entityPolicy, "convertInteger:" , $source.active)

It continues to fail because the selector is not recognized. In particular, he gets the following error.

unrecognized selector sent to instance

I have tried many options.

  • convertInteger:
  • Convert (integer :)
  • convertInteger (_ :)

I cannot get any changes to work. What is a valid selector for this expression?

In Swift code, I put the statement in the initializer and it passes, but I cannot use the same selector in the expression for the policy.

 import CoreData @objc open class IntegerToBooleanMigrationPolicy: NSEntityMigrationPolicy { @objc public override init() { super.init() assert(responds(to: #selector(convert(integer:))), "Policy does not respond to selector!") } @objc open func convert(integer: Int16) -> Bool { debugPrint("Converting \(integer) to boolean") return integer == 1 } } 
+2
source share
1 answer

After pasting the code snippet into the Swift REPL, I evaluated the following expression:

 20> #selector(IntegerToBooleanMigrationPolicy.convert(integer:)) $R1: Selector = "convertWithInteger:" 

This means that convertWithInteger: is the selector that you should use in the match expression.

+8
source

Source: https://habr.com/ru/post/1259886/


All Articles