使用uitextfield删除按计数获取核心数据二进制数据 [英] use uitextfield delegetate to fetch coredata binary data by its count number

查看:68
本文介绍了使用uitextfield删除按计数获取核心数据二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的快捷代码将3个名称保存到核心数据实体用户名。我想使用uitextfield委托来提取特定的字符串。因此,当用户在文本字段中输入2时。在标签labelName上应出现名称jessica Biel。因此,用户在文本字段中输入数字,字符串将出现在标签上。如果为1,则将第一个NSManagedObject输入到核心数据实体userName中。链接到项目 https://github.com/redrock34/jessicaBiel

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")
    }
}

}

推荐答案

首先请注意,在每次启动该应用程序时,都会一次又一次插入三个记录,因此您将获得很多重复项。

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.

由于名称显然与任何订单都不相关,请添加唯一标识符,例如整数属性 index –或任何合理的名称–然后使用谓词 NSPredicate(format: index ==%ld,Int(enterT.text!)?? 0)提取数据。

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).

您必须这样做,因为Core Data会将对象无序保存。

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

这篇关于使用uitextfield删除按计数获取核心数据二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆