协议' ...'作为一种类型,不能符合' ...' - 通用协议问题的通用协议

发布于 2025-02-10 05:07:04 字数 1979 浏览 2 评论 0原文

我一直在和Swift和Swiftui一起玩。我一直在尝试为各种“数据实体”提出一个基于代表的数据存储,目的是能够在保持代码接口稳定的同时替换基础数据存储的实现(这是一个实验代码库,所以我想随着时间的推移使用不同的数据存储),

经过几次尝试,我得出的结论是,大部分高级工作都在重复(我最终基本上要应对锅炉平板代码的粘贴),所以我想我'd聪明,减少并重新利用概念,愚蠢的我。

因此,我从一个基本的代表开始,该代表拥有每个代表将需要能够做的“核心”/“共同”功能。

    protocol SomeDelegateProtocol {
        associatedtype Item
        
        func performActionOn(_ item: Item)
        // Lots of common actions which can
        // be performed on entities
    }

加上一个“常见的“顶级”管理器”的概念(该应用程序将通过其实现的实现 - 因此,我们正在解耦层)

    class SomeManager<Delegate: SomeDelegateProtocol> {
        typealias Item = Delegate.Item
        
        let delegate: Delegate

        @Published var items: [Delegate.Item]
        // Other properties
        
        init(delegate: Delegate) {
            self.delegate = delegate
        }
        
        // Lots of boiler plate functionality
    }

,对于每个实体,我们需要定义一个目标的代表和经理

    protocol OtherItemProtocol {
        // Item enity properties
    }
    
    protocol OtherDelegateProtocol: SomeDelegateProtocol where Item == any OtherItemProtocol {
        // Specific functionality for this delegate
    }

    class OtherManager: SomeManager<OtherDelegateProtocol> {
        // Specific functionality for type of manager
    }

,这就是我遇到问题的地方。在我的其他管理器上,Xcode/swift抱怨:

协议的“其他DelegateProtocol”作为一种类型不能符合“某种程度上的交易”

Swift 5.7下的“某种程度上的协议”,我得到的错误略有不同

键入'任何其他DelegateProtocol'不能符合'某种程度上的Protocol`

对于(一点点)清晰度, 。大多数应用程序代码将使用someManager的实现,例如,需要处理otheritemprotocol的代码将直接通过extermanager,不关心“如何存储”实体。这种方法旨在减少“重复”代码的数量。

我花了几天时间试图解决这个问题,但是我似乎找不到与这个特定问题相匹配的解决方案。我可能会在角落里哭一点...如果有人对我如何解决这个问题有一些想法,我会很感激

I've been playing around with Swift and SwiftUI. I've been trying to come up with a delegate based data store for various "data entities", the intention been to be able to replace the underlying data store implementations while keeping the code interface stable (this is an experimental code base, so I want to play with different data stores over time)

After a few attempts I came to the conclusion that much of the top level work was repeating (and I'd end up basically coping an pasting the boiler plate code), so, I thought I'd be clever and reduce and re-use the concepts, stupid me.

So, I started with a basic delegate, which holds the "core"/"common" functionality each delegate will need to be able to do.

    protocol SomeDelegateProtocol {
        associatedtype Item
        
        func performActionOn(_ item: Item)
        // Lots of common actions which can
        // be performed on entities
    }

Together with a concept of a "common" top level "manager" (implementations of which the app will work through - so we're decoupling the layers)

    class SomeManager<Delegate: SomeDelegateProtocol> {
        typealias Item = Delegate.Item
        
        let delegate: Delegate

        @Published var items: [Delegate.Item]
        // Other properties
        
        init(delegate: Delegate) {
            self.delegate = delegate
        }
        
        // Lots of boiler plate functionality
    }

Now, for each entity, we need to define a targeted delegate and manager

    protocol OtherItemProtocol {
        // Item enity properties
    }
    
    protocol OtherDelegateProtocol: SomeDelegateProtocol where Item == any OtherItemProtocol {
        // Specific functionality for this delegate
    }

    class OtherManager: SomeManager<OtherDelegateProtocol> {
        // Specific functionality for type of manager
    }

And this is where I run into problems. On my OtherManager, Xcode/Swift is complaining that:

Protocol 'OtherDelegateProtocol' as a type cannot conform to 'SomeDelegateProtocol'

Under Swift 5.7, I get a slightly different error

Type 'any OtherDelegateProtocol' cannot conform to 'SomeDelegateProtocol`

Just for (a little bit of) clarity. The majority of the app code will use implementations of SomeManager, for example, the code which needs to deal with OtherItemProtocol would work directly through OtherManager, not caring about "how" the entity is been stored. This approach is intended to reduce the amount of "repeating" code.

I've spent a couple of days trying to get my head around this issue, but I can't seem to find a solution which matches this particular problem. I'll probably just cry in the corner for a bit ... if any one has some ideas on how I might be able to resolve this, I'd appreciate it ????

(Yes, I've been Googling, but I've not found anything which matches a simular structure to what I've "dreamt" up)

Swift Swift

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2025-02-17 05:07:04

您所写的是速记的

class OtherManager: SomeManager<any OtherDelegateProtocol> { }

,但协议不符合自己。 任何其他其他问题都不符合:某种程度上的gateprotocol要求。您需要一个具体的某种程度上的eLegateProtocol类型。

要表示:

class OtherManager<Delegate: OtherDelegateProtocol>: SomeManager<Delegate> {

另外,item ==任何其他eThotemProtocol感觉就像Objective-C代码。也许你是说这个?

protocol OtherDelegateProtocol: SomeDelegateProtocol where Item: OtherItemProtocol {

您甚至需要其他DelegateProtocol而不是扩展名吗?

extension SomeDelegateProtocol where Item: OtherItemProtocol {
class OtherManager<Delegate: SomeDelegateProtocol>: SomeManager<Delegate> where Delegate.Item: OtherItemProtocol {

您甚至需要一个子类吗?通常,没有人在Swift中需要一个子类。

extension SomeManager where Item: OtherItemProtocol {

What you've written is shorthand for

class OtherManager: SomeManager<any OtherDelegateProtocol> { }

But protocols do not conform to themselves. any OtherDelegateProtocol does not meet the : SomeDelegateProtocol requirement. You need a concrete SomeDelegateProtocol type, instead.

To express that:

class OtherManager<Delegate: OtherDelegateProtocol>: SomeManager<Delegate> {

Also, Item == any OtherItemProtocol feels like Objective-C code. Maybe you meant this?

protocol OtherDelegateProtocol: SomeDelegateProtocol where Item: OtherItemProtocol {

Do you even need OtherDelegateProtocol, instead of an extension?

extension SomeDelegateProtocol where Item: OtherItemProtocol {
class OtherManager<Delegate: SomeDelegateProtocol>: SomeManager<Delegate> where Delegate.Item: OtherItemProtocol {

Do you even need a subclass? Typically, nobody ever needs a subclass in Swift.

extension SomeManager where Item: OtherItemProtocol {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文