I have an custom struct called 'News' that I want to append into the array to NSUserDefault. But it's showing error "Type 'News' does not conform to protocol 'AnyObject'".
I don't want to change the 'News' struct to a class since it's being used for other code already. Is there anyway that I can change NSUserDefaults.standardUserDefaults().arrayForKey("savedNewsArray") type to [News]?
var savedNews = NSUserDefaults.standardUserDefaults().arrayForKey("savedNewsArray")
var addSavedNews = savedNews as? [News]
addSavedNews.append(News(id: "00", title: newsTitle, source: source, imageURL: imageURL, url: url))
NSUserDefaults.standardUserDefaults().setObject(addSavedNews, forKey: "savedNewsArray")
NSUserDefaults.standardUserDefaults().synchronize()
Here is the 'News' struct.
public struct News {
public var id: String
public var title: String
public var source: String?
public var imageURL: String?
public var date: NSDate?
public var url: String
init(id: String, title: String, source: String, imageURL: String, url: String) {
self.id = id
self.title = title
self.source = source
self.imageURL = imageURL
self.url = url
}
}