So I know there are many other similar questions about this type of problem, but my fried brain cells genuinely cannot, for the life of me, figure out how to solve the following error: "Value of type 'Article' has no subscripts". I tried applying a lot of other solutions from other Stack Overflow posts, but the issue still persists.
Brief overview about my project: I'm trying to use a news API for some intended application. However, there are a lot of duplicate news articles within my array -- that I'm trying to remove.
So here is my API Call, works like a charm:
let topic_endpoint = "blah blah endpoint + secret api_key"
guard let url = URL(string: topic_endpoint) else {
print("Error creating url object")
return
}
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request){ data, _, error in
if error != nil {
print((error?.localizedDescription)!)
return
}
if let data = data {
let response = try! JSON(data: data)
for index in response["articles"]{
let id = index.1["publishedAt"].stringValue
let title = index.1["title"].stringValue
let description = index.1["description"].stringValue
let image = index.1["urlToImage"].stringValue
let url = index.1["url"].stringValue
DispatchQueue.main.async {
//print(response)
if self.getSentimentFromBuildInAPI(text: title) == 1{
self.articles.append(Article(id: id, title: title, description: description, image: image, url: url))
}
}
}
}
}.resume()
And here is my removeDuplications function:
I first iterate throughout the entire array of type Article, and then add the title into a new array called 'usedNames'. At every step, I check to see if any other title is already used and added, and if it does I skip over that value to my array again. Hopefully, I explained this well, please let me know if I need to change anything.
func removeDuplications(){
//haven't used title yet, this is was just something I tried looking off another stackoverflow post.
guard let title = self.articles["title"] as? [[String: Any]] else{
print("error: dictionary type not recognized")
}
for index in self.articles{
if(!self.usedNames.contains(index["title"])){ //<----------this is where I get my error: 'Value of type 'Article' has no subscripts'
//Tried reappending the new titled articles (below) into my Published dictionary/array, is also showing the same issue as above
//self.articles.append(Article(id: index["id"], title: index["title"], description: index["description"], image: index["image"], url: index["url"]))
self.usedNames.append("title")
}
}
}
Thanks so much for helping me with this, really appreciate it:)