How can I go about checking if Core Data contains X data?

So I call FetchRequest to get the data.

Code Block
     @FetchRequest(fetchRequest: FavoriteItem.getAllFavorites()) var isFavorite:FetchedResults<FavoriteItem>


When I print out isFavorite I can see all the data in my coredata model, but I'm not sure how I can run a contain that data.

This is what I thought would work

Code Block
if isFavorite.contains(foo.name) {
print("it's already there nerd")
}


I get the error Cannot convert value of type 'String' to expected argument type 'FetchedResults<FavoriteItem>.Element' (aka 'FavoriteItem')

Do I need to do a foreach and check each item?
Answered by OOPer in 635198022

Here's the definition

Thanks for showing the definition.

Assuming you want to find a FavoriteItem with its name property matching the String value foo.name, you can write something like this:
Code Block
if favoriteRunClubItem.contains(where: {$0.name == foo.name}) {
print("it's already there nerd")
}


You should better re-consider the naming of isFavorite. I do not think it is an appropriate name for a collection of FavoriteItem.


I guess foo.name is a String value. What sort of String properties are defined in FavoriteItem?
Please show the definition of FavoriteItem.
Yes, check all key of the dictionary.

Or write an extension to Dictionary, that tests if some element in contained in one element of the dictionary (looping through dict.keys).

Such as

Code Block
extension Dictionary where Key == String, Value: AnyObject {
func contains(_ someString: String) -> Bool {
for key in self.keys {
if let val = self[key] as? String {
if val.contains(someString) { return true }
}
}
return false
}
}
let dict : [String: AnyObject] = ["a": "Hey" as AnyObject, "b": "Hello" as AnyObject, "c": "You" as AnyObject, "d": true as AnyObject]
print(dict.contains("Yo"))


Even simpler with Any instead of AnyObject:
Code Block
extension Dictionary where Key == String, Value: Any {
func contains(_ someString: String) -> Bool {
for key in self.keys {
if let val = self[key] as? String {
if val.contains(someString) { return true }
}
}
return false
}
}
let dict : [String: Any] = ["a": "Hey", "b": "Hello", "c": true]
print(dict.contains("Yo"))


Note: you could also return (String, Bool), to know which key contains the String (String would be "" if no match)
Thanks OOPer for the feedback. I've gone ahead and changed isFavorite to favoriteRunClubItem

Here's the definition

Code Block
public class FavoriteItem:NSManagedObject, Identifiable {
  @NSManaged public var isFavorite:Bool
  @NSManaged public var misc:String?
  @NSManaged public var name:String?
  @NSManaged public var location:String?
  @NSManaged public var date:String?
  @NSManaged public var category:String?
  @NSManaged public var dayofweek:Int
  @NSManaged public var link:String?
  @NSManaged public var hour:Int
  @NSManaged public var minute:Int
}
extension FavoriteItem {
  static func getAllFavorites() -> NSFetchRequest<FavoriteItem>{
    let request:NSFetchRequest<FavoriteItem> = FavoriteItem.fetchRequest() as! NSFetchRequest<FavoriteItem>
     
    let sortDescriptor = NSSortDescriptor(key: "category", ascending: true)
    request.sortDescriptors = [sortDescriptor]
    return request
  }
}

Accepted Answer

Here's the definition

Thanks for showing the definition.

Assuming you want to find a FavoriteItem with its name property matching the String value foo.name, you can write something like this:
Code Block
if favoriteRunClubItem.contains(where: {$0.name == foo.name}) {
print("it's already there nerd")
}


How can I go about checking if Core Data contains X data?
 
 
Q