Bre*_*nan 3 core-data ios swift swift3
我正在尝试使用Core Data实现非常基本的自定义迁移.最初将单个属性创建为Integer 16
值为0或1.在新模型版本中,此属性已更改为,Boolean
并且下面的迁移策略应处理它.我看过几个用Swift编写的例子,它们似乎没有使可访问性公开/公开或添加@objc
以使Objective-C可以访问它.我这样做是为了消除任何不起作用的原因.
我使用以下表达式为实体映射创建了映射模型和自定义策略.
FUNCTION($entityPolicy, "convertInteger:" , $source.active)
它一直失败,因为无法识别选择器.具体来说,它会收到以下错误.
unrecognized selector sent to instance
我尝试了很多变化.
我无法获得任何变化.这个表达式的有效选择器是什么?
在Swift代码中,我在初始化程序中放置了一个断言并且它通过了,但是我不能在策略的表达式中使用相同的选择器.
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
}
}
Run Code Online (Sandbox Code Playgroud)
bda*_*ash 12
将代码片段粘贴到Swift REPL后,我评估了以下表达式:
20> #selector(IntegerToBooleanMigrationPolicy.convert(integer:))
$R1: Selector = "convertWithInteger:"
Run Code Online (Sandbox Code Playgroud)
这表明convertWithInteger:
你应该在映射表达式中使用的选择器.