Pisan
Pisan

Reputation: 69

Swift: Search for string in core data model

I'm confronted with the following problem in Swift.

I wrote the following function to retrieve all persons in my model with the 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)

        for result in results
        {
             print(result)
        }

    } catch let error{
        print(error)
    }
}

Even though my model contains two entries with the attribute name = "David", the line

myRequest.predicate = NSPredicate(format: "name = %@", "David")

does not find these entries, because "name" is an optional attribute in my model. Do I have to search for Optional("David"), meaning something like myRequest.predicate = NSPredicate(format: "name = %@", Optional("David"))?

How can I search for all entries with the name "David" in my model? What am I doing wrong?

Thanks for your help!

Upvotes: 5

Views: 10339

Answers (2)

Kunjuzz
Kunjuzz

Reputation: 1

         @IBAction func searchbtn(_ sender: Any) {

        
            let request = NSFetchRequest<NSFetchRequestResult>(entityName: "StudentDetails")
            let searchstring = self.searchbar.text
            request.predicate = NSPredicate(format: "rollno == %@", searchstring!)
            var outputstr = ""
            do
            {
                let result = try context.fetch(request)
                if result.count > 0
                {
                    for oneline in  result
           
                {
                    
                    oneroll = (oneline as AnyObject).value(forKey: "rollno") as! String
                       onename = (oneline as AnyObject).value(forKey: "name") as! String
                  onemarks = (oneline as AnyObject).value(forKey: "mark") as! String
                                           
                /*outputstr += oneroll + ""
                        outputstr += onename + ""
                        outputstr += onemarks + ""*/
                    }
                }
                else {
                    outputstr = " no match found"
                }
                self.searchbar.text = outputstr
                self.bgvhgvhgvyg.text = oneroll
                self.nma_fld.text = onename
                self.mrk_fild.text = onemarks
            }
            catch{
                print("Error")
            }
        }
        

Upvotes: 0

Nirav D
Nirav D

Reputation: 72410

You are predicating with string so enclose your comparison name with single quote like this

myRequest.predicate = NSPredicate(format: "name = '%@'", "David")  

Also try with Contains

myRequest.predicate = NSPredicate(format: "name CONTAINS[cd] %@", "David") 

Upvotes: 12

Related Questions