I have a property called savedArticles
that I would like to observe. As of now, it is declared as a Set as a property of a Controller type object as shown below:
@objc dynamic var savedArticles: Set<Article> = []
This does not work because "property cannot be marked @objc
because its type cannot be represented in Objective-C".
How should I changed savedArticles
so that it can still be used as a set but also be observable?
My first thought was to change the type of my property to NSSet but I couldn't manage to get this working and wasn't even sure if it was the right approach or not. Using this approach, I was forced to manually implelemnt the Hashable protocol for Article
. I did that as follows
class Article: Hashable {
static func == (lhs: Article, rhs: Article) -> Bool {
return lhs.title == rhs.title && lhs.date == rhs.date && lhs.author == rhs.author
}
func hash(into hasher: inout Hasher) {
title.hash(into: &hasher)
date.hash(into: &hasher)
author.hash(into: &hasher)
}
var title: String?
var author: String?
var description: String?
var date: String?
}