如何获得对象保存数量的计数

时间:2018-08-02 18:31:37

标签: swift

我创建了一个联系人列表,将联系人保存在核心数据中。我可以按名称访问该联系人,但想知道如何获取到目前为止已保存的联系人数。

我认为托管对象上下文将能够提供硬币,因为这是为添加的每个新联系人创建空间的原因,但事实并非如此,或者我使用的是错误的。

这是代码:

import UIKit
import CoreData

class ViewController: UIViewController {

    let managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    @IBOutlet weak var name: UITextField!
    @IBOutlet weak var address: UITextField!
    @IBOutlet weak var phone: UITextField!

    @IBOutlet weak var status: UILabel!

    @IBAction func saveContact(_ sender: UIButton) {

        let entityDescription = NSEntityDescription.entity(forEntityName: "Contacts", in: managedObjectContext)
        let contact = Contacts(entity: entityDescription!, insertInto: managedObjectContext)

        contact.name = name.text!
        contact.address = address.text!
        contact.phone = phone.text!
        do {
            try managedObjectContext.save()
            name.text = ""
            address.text = ""
            phone.text = ""
            status.text = "Contact Saved"
        } catch let error {
            status.text = error.localizedDescription               
        }          
    }

    @IBAction func findContact(_ sender: UIButton) {           
        let entityDescription = NSEntityDescription.entity(forEntityName: "Contacts",
                                                           in: managedObjectContext)
        let request: NSFetchRequest<Contacts> = Contacts.fetchRequest()
        request.entity = entityDescription
        let pred = NSPredicate(format: "(name = %@)", name.text!)
        request.predicate = pred
        do {
            var results =
                try managedObjectContext.fetch(request as! NSFetchRequest<NSFetchRequestResult>)
            if results.count > 0 {
                let match = results[0] as! NSManagedObject
                name.text = match.value(forKey: "name") as? String
                address.text = match.value(forKey: "address") as? String
                phone.text = match.value(forKey: "phone") as? String
                status.text = "Matches found: \(results.count)"
            } else {
                status.text = "No Match"
            }
        } catch let error {
            status.text = error.localizedDescription
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以使用

获取实体的对象数
do {
    let request: NSFetchRequest<Contacts> = Contacts.fetchRequest()
    let numberOfContacts = try managedObjectContext.count(for: request)
    print(numberOfContacts)
} catch { print(error) }

旁注:您的findContact方法非常麻烦。这是一个改进的版本

@IBAction func findContact(_ sender: UIButton) {

    let request: NSFetchRequest<Contacts> = Contacts.fetchRequest()
    request.predicate = NSPredicate(format: "name = %@", name.text!)
    do {
        let results = try managedObjectContext.fetch(request)
        if let match = results.first {
            name.text = match.name
            address.text = match.address
            phone.text = match.phone
            status.text = "Matches found: \(results.count)"
        } else {
            status.text = "No Match"
        }
    } catch {
        status.text = error.localizedDescription

    }
}