use uitextfield delegetate to fetch coredata binary data by its count number

一曲冷凌霜 提交于 2020-02-06 07:41:25

问题


My swift code saves 3 names to core data entity "username". I want to use uitextfield delegate to pull a specific string. So when the user enters 2 in the textfield. On the label labelName the name jessica Biel should appear. So the user enters a number into a textfield a string appears on the label. If number 1 is enter the 1st NSManagedObject into the core data entity userName. Link to project https://github.com/redrock34/jessicaBiel

import UIKit
import CoreData

class ViewController: UIViewController,uitextfielddele {
@IBOutlet var labelName : UILabel!
@IBOutlet var enterT : UITextField!


// MARK: Variables declearations
let appDelegate = UIApplication.shared.delegate as! AppDelegate //Singlton instance
var context:NSManagedObjectContext!

// MARK: View Controller life cycle methods
override func viewDidLoad() {
    super.viewDidLoad()

    openDatabse()
}

// MARK: Methods to Open, Store and Fetch data
func openDatabse()
{
    context = appDelegate.persistentContainer.viewContext
    let entity = NSEntityDescription.entity(forEntityName: "Users", in: context)
    let newUser = NSManagedObject(entity: entity!, insertInto: context)
    let newUser2 = NSManagedObject(entity: entity!, insertInto: context)
    let newUser3 = NSManagedObject(entity: entity!, insertInto: context)
    saveData(UserDBObj: newUser, UserDBObj2: newUser2, UserDBObj3: newUser3)

}

func saveData(UserDBObj:NSManagedObject,UserDBObj2:NSManagedObject,UserDBObj3:NSManagedObject)
{
    UserDBObj.setValue("kim kardashian", forKey: "username")
    UserDBObj2.setValue("jessica biel", forKey: "username")
    UserDBObj3.setValue("Hailey Rienhart", forKey: "username")




    print("Storing Data..")
    do {
        try context.save()
    } catch {
        print("Storing data Failed")
    }

    fetchData()
}

func fetchData()
{
    print("Fetching Data..")
    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
    request.returnsObjectsAsFaults = false
    do {
        let result = try context.fetch(request)
        for data in result as! [NSManagedObject] {
            let userName = data.value(forKey: "username") as! String

            print("User Name is : "+userName)
        }
    } catch {
        print("Fetching data Failed")
    }
}

}


回答1:


First of all be aware that on each launch of the app the three records are inserted again and again so you'll get a bunch of duplicates.

As the names are apparently not related to any order add an unique identifier like an integer attribute index – or whatever name is reasonable – and then fetch the data with a predicate NSPredicate(format: "index == %ld", Int(enterT.text!) ?? 0).

You have to do that because Core Data saves the objects unordered.



来源:https://stackoverflow.com/questions/59698875/use-uitextfield-delegetate-to-fetch-coredata-binary-data-by-its-count-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!