I was trying since few days ago to encode and read data by types but I'm unable to do it without some help.
Right now I'm able to get count or title with this lines:
playlist?.objects.count ?? 0
playlist?.objects[indexPath.item].title
What I want to archive is something like this.
playlist?.objects.TYPEIWANT.count ?? 0
playlist?.objects.TYPEIWANT[indexPath.item].title
Here is my code:
class Playlist: Equatable, Codable {
init(name: String, objects: [Object] = []) {
self.name = name
self.objects = objects
}
// MARK: Properties
let name: String
var objects: [Object]
}
func ==(lhs: Playlist, rhs: Playlist) -> Bool {
return lhs.name == rhs.name && lhs.objects == rhs.objects
}
class Object: Equatable, Codable {
enum Group: String, Codable {
case LiveChannel
case TelevisionShow
case Movie
case unsupported
static let allGroups: [Group] = [.LiveChannel, .TelevisionShow, .Movie, .unsupported]
}
init(type: Group.RawValue, title: String, group: String, url: String, logo: String) {
self.type = type
self.title = title
self.group = group
self.url = url
self.logo = logo
}
// MARK: Properties
let type: String
let title: String
let group: String
let url: String
let logo: String
}
func ==(lhs: Object, rhs: Object) -> Bool {
return lhs.title == rhs.title && lhs.group == rhs.group
}
extension Object {
static func create(type: String, title: String, group: String, url: String, logo: String, playlist: Playlist) {
let object = Object(type: type, title: title, group: group, url: url, logo: logo)
ObjectsController.shared.add(object: object, toPlaylist: playlist)
}
}
When an object is created the type is stored.
Object.create(type: Object.Group.TelevisionShow.rawValue, title: title, group: loadedGroup, url: line, logo: match(for: "\".*tvg-logo=\"([^\"]*)", in: line), playlist: playlist)
But I don't know how to get or manage just one Group type.