我正在尝试创建一个例程来简化将一个属性绑定到另一个属性,这是一个非常常见的操作。我在Swift 4和XCode 9中使用基于块的KVO。
我希望能够使用相应的keyPath
编写以下内容来绑定两个变量:
self.bind(to: \BindMe.myFirstName, from: \BindMe.person.firstName )
这是一个简化的例子,它产生了我无法解决的各种编译错误。可能是将keyPath错误地传递给func bind
,但使用keyPath的setValue
也无法编译。请参阅代码中的注释,了解我遇到的编译错误。
class Person : NSObject
{
init( firstName:String, lastName:String )
{
self.firstName = firstName
self.lastName = lastName
}
@objc dynamic var firstName:String
@objc dynamic var lastName:String
}
class BindMe : NSObject
{
var observers = [NSKeyValueObservation]()
let person:Person
var myFirstName:String = "<no first name>"
var myLastName:String = "<no last name>"
init( person:Person )
{
self.person = person
self.setupBindings()
}
func setupBindings()
{
self.bind(to: \BindMe.myFirstName, from: \BindMe.person.firstName )
self.bind(to: \BindMe.myLastName, from: \BindMe.person.lastName )
}
// this func declaration is likely incorrect
func bind<T,Value,Value2>( to targetKeyPath:KeyPath<T,Value>, from sourceKeyPath:KeyPath<T,Value2>)
{
// Error: Generic parameter 'Value' could not be inferred
self.observers.append( self.observe(sourceKeyPath, options: [.initial,.new], changeHandler: { (object, change) in
// Error: Cannot convert value of type 'KeyPath<T, Value>' to expected argument type 'String'
self.setValue(change.newValue, forKeyPath: targetKeyPath)
}))
}
}
修改
answer below有助于解决初始编译问题。但是,为了使它有用,我需要能够将管道推入超类,如下所示。这将使该类使用它非常简单,但我仍然在努力解决编译错误:
Cannot invoke 'bind' with an argument list of type '(to: WritableKeyPath<PersonWatcher, PersonWatcher>, from: WritableKeyPath<PersonWatcher, PersonWatcher>)'
如果我将泛型类型T传递给绑定例程,我会收到此错误:
Type 'BindBase' has no subscript members
class BindBase :NSObject
{
var observers = [NSKeyValueObservation]()
func bind<Value>(to targetKeyPath: ReferenceWritableKeyPath<BindBase, Value>, from sourceKeyPath: KeyPath<BindBase, Value>)
{
self.observers.append(self.observe(sourceKeyPath, options: [.initial, .new], changeHandler: { (object, change) in
self[keyPath: targetKeyPath] = change.newValue!
}))
}
}
class PersonWatcher : BindBase
{
@objc dynamic var person: Person
@objc var myFirstName: String = "<no first name>"
@objc var myLastName: String = "<no last name>"
init(person: Person) {
self.person = person
super.init()
self.bind(to: \PersonWatcher.myFirstName, from: \PersonWatcher.person.firstName)
self.bind(to: \PersonWatcher.myLastName, from: \PersonWatcher.person.lastName)
}
}
答案 0 :(得分:1)
根据接受的提案SE-0161 Smart KeyPaths: Better Key-Value Coding for Swift,您需要使用ReferenceWritableKeyPath
使用下标将值写入具有引用语义的对象的键路径。
(您需要将经典的基于String
的关键路径传递给setValue(_:forKeyPath:)
,而不是KeyPath
。)
还有一些:
Value
和Value2
需要与分配相同T
需要表示self
@objc
BindMe.init(person:)
需要super.init()
所以,你的BindMe
会是这样的:
class BindMe: NSObject {
var observers = [NSKeyValueObservation]()
@objc let person: Person
@objc var myFirstName: String = "<no first name>"
@objc var myLastName: String = "<no last name>"
init(person: Person) {
self.person = person
super.init()
self.setupBindings()
}
func setupBindings() {
self.bind(to: \BindMe.myFirstName, from: \BindMe.person.firstName)
self.bind(to: \BindMe.myLastName, from: \BindMe.person.lastName)
}
func bind<Value>(to targetKeyPath: ReferenceWritableKeyPath<BindMe, Value>, from sourceKeyPath: KeyPath<BindMe, Value>) {
self.observers.append(self.observe(sourceKeyPath, options: [.initial, .new], changeHandler: { (object, change) in
self[keyPath: targetKeyPath] = change.newValue!
}))
}
}
编辑:
制作BindBase
之类似物的要求似乎非常合理,所以我做了一些尝试。
履行
T
需要表示self
(其中T == KeyPath.Root
),使用Self
将是最本能的,但不幸的是,它的使用在当前版本的Swift中仍然非常有限。
您可以使用bind
将Self
的定义移至协议扩展名中:
class BindBase: NSObject, Bindable {
var observers = [NSKeyValueObservation]()
}
protocol Bindable: class {
var observers: [NSKeyValueObservation] {get set}
}
extension Bindable {
func bind<Value>(to targetKeyPath: ReferenceWritableKeyPath<Self, Value>, from sourceKeyPath: KeyPath<Self, Value>)
where Self: NSObject
{
let observer = self.observe(sourceKeyPath, options: [.initial, .new]) {object, change in
self[keyPath: targetKeyPath] = change.newValue!
}
self.observers.append(observer)
}
}