2017-11-08 6 views
0

과 함께 나는 값KVC associatedtype

extension Bindable { 

    func changeToValue(_ value: PropertyType) { 
     boundObject?[keyPath: propertyPath] = value 
    } 
} 

을 변경하기위한 기본 구현을하려는 바인딩 프로토콜을

protocol Bindable: class { 
    associatedtype ObjectType: Any 
    associatedtype PropertyType 

    var boundObject: ObjectType? { get set } 
    var propertyPath: WritableKeyPath<ObjectType, PropertyType>? { get set } 

    func changeToValue(_ value: PropertyType) 
} 

을하지만이 없다는 오류가 발생합니다 :

유형 'Self.ObjectType'에는 아래 첨자가 없습니다

propertyPath의 정의는 ObjectType의 경우 KeyPath이므로 여기에 무슨 일이 일어나고있는 것입니까? 어떻게하면 propertyPath가 실제로 변경된 객체의 keyPath인지 컴파일러에 알릴 수 있습니다.

답변

2

propertyPath을 선택하지 않아야한다고 생각하지 않습니다. 이게 작동해야합니다 :

protocol Bindable: class { 
    associatedtype ObjectType: Any 
    associatedtype PropertyType 

    var boundObject: ObjectType? { get set } 
    var propertyPath: WritableKeyPath<ObjectType, PropertyType>{ get set } 

    func changeToValue(_ value: PropertyType) 
} 

extension Bindable { 

    func changeToValue(_ value: PropertyType) { 
     boundObject?[keyPath: propertyPath] = value 
    } 
} 
+1

당신이 맞습니다. 선택적인'KeyPath'는 문제였습니다. 그것은 매우 혼란스러운 오류입니다. –

+0

@ViktorKucera : 오류 설명이 매우 혼란 스럽습니다. –