I have 2 simple structs in my app that are conforming to a protocol, to enable some generic operations I need to apply on both types:
import UIKit
protocol Track {
var url: URL { get set }
}
struct TrackFromMediaFile: Track {
var url: URL
}
struct TrackFromAsset: Track {
var url: URL
}
Now I have a list of these structs:
var tracks = [Track]()
tracks.append(TrackFromMediaFile(url: URL(string: "http://www.remotefile")!))
tracks.append(TrackFromAsset(url: URL(string: "local://localfile")!))
I need to find the index of a specific track in this list of tracks:
// throws:
// Value of protocol type 'Track' cannot conform to 'Equatable'; only struct/enum/class types can conform to protocols
var track = tracks[0]
tracks.firstIndex(of: track)
I tried to follow approaches from https://stackoverflow.com/a/46719045/319905 and https://developer.apple.com/videos/play/wwdc2015/408/ to no avail, I couldn't adapt these to my example.
How can I implement handling arrays of tracks in a generic way?