swift - Why do I get an NSInvalidArgument Exception, reason: 'Unable to parse the format string' -
although there ton of threads subject, haven't been able find answer problem. when refactored function below pass search argument nspredicate instead of hard coding search value, error:
*** terminating app due uncaught exception 'nsinvalidargumentexception', reason: 'unable parse 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 { let results = try managedobjectcontext.executefetchrequest(fetchrequest) as? [formula] if (results != nil) { formula.text = results?[0].formulaname } } catch { fatalerror("error fetching data!") } return }
even though think syntax correct, i've tried every other suggestion i've found here on , in apple's docs:
let predicate = nspredicate(format: "formulaname = 'baechle'") // started , learned bad idea, worked let predicate = nspredicate(format: "formulaname = %0", name) // 1 '=' results in error i've described let predicate = nspredicate(format: "formulaname == %0", name) // 2 '==', 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. appreciated.
because have been using wrong format specifier. should %@
instead of %0
:
let predicate = nspredicate(format: "formulaname == %@", name)