Core Data自定义策略的选择器语法是什么? [英] What is the selector syntax for Core Data custom policies?
问题描述
我正在尝试使用Core Data实现非常基本的自定义迁移.最初创建的单个属性为Integer 16
,其值为0或1.在新的模型版本中,此属性更改为Boolean
,并且下面的迁移策略应对此进行处理.我已经看到了几个用Swift编写的示例,它们似乎并没有使可访问性打开/公开,也没有添加@objc
使其可被Objective-C访问.我这样做是为了消除它无法正常工作的任何原因.
I am trying to implement a very basic custom migration with Core Data. A single property was initially created as an Integer 16
with values of 0 or 1. In the new model version this property is changed to Boolean
and the migration policy below should handle it. I have seen several examples written in Swift and they don't appear to make the accessibility open/public or add @objc
to make it accessible to Objective-C. I have done this to eliminate any reason it not working.
我已使用以下表达式为用于实体映射的自定义策略创建了映射模型.
I've created the mapping model with the custom policy for the entity mapping with the following expression.
FUNCTION($entityPolicy, "convertInteger:" , $source.active)
由于无法识别选择器而一直失败.具体来说,它会收到以下错误.
It keeps failing because the selector is not recognized. Specifically it gets the following error.
unrecognized selector sent to instance
我尝试了很多变化.
- convertInteger:
- 转换(整数:)
- convertInteger(_:)
我无法进行任何改动.此表达式的有效选择器是什么?
I am unable to get any variation to work. What is a valid selector for this expression?
在Swift代码中,我在初始化器中放置了一个断言,该断言通过了,但是我不能在策略的表达式中使用相同的选择器.
In the Swift code I put an assertion in the initializer and it passes, but I cannot use that 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
}
}
推荐答案
将代码段粘贴到Swift REPL之后,我评估了以下表达式:
After pasting your code snippet into the Swift REPL, I evaluated the following expression:
20> #selector(IntegerToBooleanMigrationPolicy.convert(integer:))
$R1: Selector = "convertWithInteger:"
这表明convertWithInteger:
是您应在映射表达式中使用的选择器.
That suggests convertWithInteger:
is the selector you should use in the mapping expression.
这篇关于Core Data自定义策略的选择器语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!