这是将 属性 标识为“@Published”的适当协议约束

Which is an appropriate protocol constraint to identify a property as being '@Published'

我想为集合中的对象建立绑定,以便我可以像引用指针一样将它传递到下游。我希望这是一个方便的功能,但我遇到了一些麻烦。

我可以对以下协议施加什么约束(如果有的话)'Diskable' 以消除我遇到的错误?

final class PropertiesData: ObservableObject, Diskable {
    
    @Published var fetchedItems: [Property] = []
        
    var location: URL {
        let file = try! LocalStorage.rootFolder.createFileIfNeeded(withName: "properties.data")
        return file.url
    }
}

extension Diskable where T: Identifiable {
    
    func binding(for object: T) -> Binding<T> {
        let index = fetchedItems.firstIndex(where: { [=11=].id == object.id } )!
        return $fetchedItems[index] // cannot find 'fetchedItems' in scope
    }
}

protocol Diskable: AnyObject {
    associatedtype T: Codable
    var location: URL { get }
    var fetchedItems: [T] { get set }
}

就我可以复制您的代码而言,这是一个解决方案(使用 Xcode 12.1 / iOS 14.1 测试)

extension Diskable where T: Identifiable {
    func binding(for object: T) -> Binding<T> {
        let index = fetchedItems.firstIndex(where: { [=10=].id == object.id } )!
        return Binding(
            get: { self.fetchedItems[index] },
            set: { self.fetchedItems[index] = [=10=] }
        )
    }
}