Swift Generic:未找到数组的附加

发布于 2025-01-20 00:35:21 字数 587 浏览 4 评论 0原文

我第一次尝试使用 swift 泛型:

extension RealmSwift.List where Element == Object {
    // @deprecated use RealmSwift.List<>
    func arrayo<T: Object>() -> [T] {
        var res: [T] = []
        for card in self {
            res.append(card) <- here I got 

调用实例方法“append”时没有完全匹配

        }
        return res
    }

    convenience init<T: Object>(objects: [T]) {
        self.init()
        for card in objects {
            append(card)
        }
    }
}

一劳永逸地编写此适配器的好方法是什么?

My first attempt to use swift generics:

extension RealmSwift.List where Element == Object {
    // @deprecated use RealmSwift.List<>
    func arrayo<T: Object>() -> [T] {
        var res: [T] = []
        for card in self {
            res.append(card) <- here I got 

No exact matches in call to instance method 'append'

        }
        return res
    }

    convenience init<T: Object>(objects: [T]) {
        self.init()
        for card in objects {
            append(card)
        }
    }
}

what's a good way to write this adapter once and for all?

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

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

发布评论

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

评论(1

嘿哥们儿 2025-01-27 00:35:21

注意 where 元素。您可以使用Element引用列表项的类型,因此不需要设置另一个类型参数Tcard 的类型为 Element 而不是 T,因此您无法将其添加到 Array 中。无法保证 TElement 等效,因此编译器不允许这样做。这同样适用于您的方便 init。

extension RealmSwift.List where Element == Object {
    // @deprecated use RealmSwift.List<>
    func arrayo() -> [Element] {
        var res: [Element] = []
        for card in self {
            res.append(card) // Now you are adding an `Element` to the array of `Element` so it will work.
        }
        return res
    }

    convenience init(objects: [Element]) {
        self.init()
        for card in objects {
            append(card)
        }
    }
}

但泛型在这里并没有多大用处,因为您已经将 Element 限制为 Object。因此只有一种潜在类型 - 您可以使 arrayo() 和 init 直接使用 Object

为了使这个有用做

extension RealmSwift.List where Elemtn: RealmCollectionValue

Notice the where Element. You can refer to the type of the list items using Element, so you do not need to set up another type parameter T. card is of type Element not T, so you cannot add it to the Array<T>. There is no guarantee that T and Element are equivalent so the compiler doesn't allow it. The same applies for your convenience init.

extension RealmSwift.List where Element == Object {
    // @deprecated use RealmSwift.List<>
    func arrayo() -> [Element] {
        var res: [Element] = []
        for card in self {
            res.append(card) // Now you are adding an `Element` to the array of `Element` so it will work.
        }
        return res
    }

    convenience init(objects: [Element]) {
        self.init()
        for card in objects {
            append(card)
        }
    }
}

But generics are not really useful here because you are constraining Element to Object already. So there is only one potential type - You could make arrayo() and the init use Object directly.

To make this useful do

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