ios - Swift: Search for string in core data model -


i'm confronted following problem in swift.

i wrote following function retrieve persons in model name "david".

private func myfetchrequest() {     let moc = (uiapplication.sharedapplication().delegate as! appdelegate).managedobjectcontext      let myrequest = nsfetchrequest(entityname: "registerofpersons")      myrequest.predicate = nspredicate(format: "name = %@", "david")      do{         let results = try moc.executefetchrequest(myrequest)          result in results         {              print(result)         }      } catch let error{         print(error)     } } 

even though model contains 2 entries attribute name = "david", line

myrequest.predicate = nspredicate(format: "name = %@", "david") 

does not find these entries, because "name" optional attribute in model. have search optional("david"), meaning myrequest.predicate = nspredicate(format: "name = %@", optional("david"))?

how can search entries name "david" in model? doing wrong?

thanks help!

you predicating string enclose comparison name single quote this

myrequest.predicate = nspredicate(format: "name = '%@'", "david")   

also try contains

myrequest.predicate = nspredicate(format: "name contains[cd] %@", "david")  

Comments