Swift 3, Extend Array, where array elements are of a specific type

I wrote a structure containing the basic data of Asset:

struct Asset: AssetProtocol {

    init (id: Int = 0, url: String) {
        self.id = id
        self.url = URL(string: url)
    }

    var id: Int 
    var url: URL?

}

He is subscribing to AssetProtocol

protocol AssetProtocol {
    var id: Int { get }
    var url: URL? { get }
}

I hope to extend Array (Sequence), where the elements in this array are elements that subscribe to AssetProtocol. Also, so that these functions can mutate the array in place.

extension Sequence where Iterator.Element: AssetProtocol {

    mutating func appendUpdateExclusive(element newAsset: Asset) {    
        ...

How can I iterate and mutate this sequence? I tried half a dozen ways and can't figure it out right!

+4
source share
1 answer

, Asset , Element. Element , AssetProtocol Element:

protocol AssetProtocol {
    var id: Int { get }
    var url: URL? { get }

    static func someStaticMethod()
    func someInstanceMethod()
}

extension Array where Element:AssetProtocol {
    mutating func appendUpdateExclusive(element newAsset: Element) {
        newAsset.someInstanceMethod()
        Element.someStaticMethod()

        append(newAsset)
    }
}
+6

Source: https://habr.com/ru/post/1658865/


All Articles