Although there are a ton of threads about this subject, I haven't been able to find an answer to my problem. When I refactored this function below to pass the search argument to NSPredicate instead of hard coding the search value, I now get the error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "formulaName == %0"'
func fetchFormula() {
let fetchRequest = NSFetchRequest(entityName: "Formula")
let calculation = calculator.selectedFormula
let name = calculation.formulaName // name = Baechle
let predicate = NSPredicate(format: "formulaName == %0", name)
fetchRequest.predicate = predicate
do {
let results = try managedObjectContext.executeFetchRequest(fetchRequest) as? [Formula]
if (results != nil) {
formula.text = results?[0].formulaName
}
} catch {
fatalError("Error fetching data!")
}
return
}
Even though I think my syntax is correct, I've tried just about every other suggestion I've found here on SO and in Apple's docs:
let predicate = NSPredicate(format: "formulaName = 'Baechle'") // started with this and learned that this is a bad idea, but it worked
let predicate = NSPredicate(format: "formulaName = %0", name) // only 1 '=' which results in the error I've described
let predicate = NSPredicate(format: "formulaName == %0", name) // now 2 '==', but still doesn't work, produces "formulaName == %0"'and same error
let predicate = NSPredicate(format: "formulaName == '%0'", name) // produces formulaName == "%0" resulting in no results
I'm out of ideas. Any help greatly appreciated.