协议扩展实施KVO投诉财产

时间:2016-02-05 11:19:59

标签: swift swift2 key-value-observing

public class EntityA: NSManagedObject {
  @NSManaged public private(set) var identifier: String
  @NSManaged public private(set) var name: String
}

public class EntityB: NSManagedObject {
  @NSManaged public private(set) var identifier: String
  @NSManaged public private(set) var name: String
}

protocol NodeRepresentable {
    var children: NSArray { get }
    static var childrenKeyPaths: [String] { get }
}


extension NodeRepresentable where Self: NSManagedObject {
    var children: NSArray { //swift compiler doesn't see this as KVC complaint
        return Self.childrenKeyPaths.flatMap { keyPath in
            return self.mutableSetValueForKey(keyPath)
        }
    } 
}

extension EntityA: NodeRepresentable {
    static var childrenKeyPaths: [String] {
        return ["events", "groups", "races"]
    }

    var _children: NSArray { //this is KVC complaint
        return self.children
    }
}

extension EntityB: NodeRepresentable {

    static var childrenKeyPaths: [String] {
        return ["events", "groups", "markets"]
    }

    var _children: NSArray { //this is KVC complaint
        return self.children
    }
}

编译器没有为EntityA和EntityB上的子项找到valueForKey。 协议扩展似乎没有实现KVO投诉属性。我已尝试在协议上声明动态属性,但这并不起作用。

是否有可能避免使用某种语言功能在每个实体上声明计算var?

1 个答案:

答案 0 :(得分:0)

Swift属性扩展对Objective-C不可见。

尝试将/pusher/auth添加到声明中。即@objc

另请参阅https://developer.apple.com/forums/thread/122181