0
votes

I am trying to filter through my CoreData for a matching Name, and if it exists ignore it, or else add the new Name to Core Data. The issue I am having should be a simple one but I can't quite figure it out.

here's a link to a shot of my code

the value in my coredata is "trackName" of type String, but I keep getting the following exception:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key trackName.'

How should I proceed? Is it not clear that the Fetch Request is for a String?

2
Don't post images. Post code! The solution is pretty simple but nobody is willing to retype the code from the screenshot. - vadian
Thanks for the feedback! I will post code next time I (inevitably) ask for more help haha - AaronBeReal

2 Answers

0
votes

You are calling value(forKey:) on an instance of NSFetchRequest, but that key is only defined for NSManagedObject instances that represent your Track entity.

A fetch request is an object that represents a Core Data query. Based on your code, I'm assuming you are trying to ensure that the trackName property is unique across all Track entities. You need to execute() the fetch request and check to make sure the array returned by that method is empty.

0
votes

Please updating your code to this one and try

if result == nil {
   //result is null because you entered string is not found the valueForkey is also not found in your result
    print("new entry")
}else{
    print("added")
}