从核心数据中获取数据并使用Swift在UITextFields中显示

时间:2019-02-10 13:01:01

标签: swift core-data

我正在尝试从Core Data中获取数据,并使用Swift在UITextFields中显示它们。我想我已经使用以下代码成功保存到了Core Data中:

    func insertUserData() {

    let user = UserData(context: managedObjectContext)
    user.full_name = nameTextField.text!
    user.address = addressTextField.text!
    user.phone_number = phoneTextField.text!
    user.email = emailTextField.text!

    do {
    try managedObjectContext.save()
        print("Saved Successfully!")
    } catch {
        print(error.localizedDescription)
    }

}

其中UserData是Core Data模型中的实体名称,而managedObjectContext是:

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

现在我正在尝试使用以下代码在另一个ViewController的UITextFields中显示保存的数据:

    func loadProfileData() {

    let entityDescription = NSEntityDescription.entity(forEntityName: "UserData", in: managedObjectContext)
    let request: NSFetchRequest<UserData> = UserData.fetchRequest()
    request.entity = entityDescription

    do {
        let userInfo = try managedObjectContext.fetch(request as! NSFetchRequest<NSFetchRequestResult>)
        let userInformation = userInfo as! NSManagedObject // I'm getting crash here: Cast from '[Any]' to unrelated type 'NSManagedObject' always fails
        nameField.text = userInformation.value(forKey: "name") as? String
        addressField.text = userInformation.value(forKey: "address") as? String
        phoneField.text = userInformation.value(forKey: "phone_number") as? String
        emailField.text = userInformation.value(forKey: "email") as? String
    } catch {
        print(error.localizedDescription)
    }

}

崩溃报告:

    libsystem_kernel.dylib`__pthread_kill:
    0x2105a70fc <+0>:  mov    x16, #0x148
    0x2105a7100 <+4>:  svc    #0x80
->  0x2105a7104 <+8>:  b.lo   0x180a6b11c               ; <+32>
    0x2105a7108 <+12>: stp    x29, x30, [sp, #-0x10]!
    0x2105a710c <+16>: mov    x29, sp
    0x2105a7110 <+20>: bl     0x180a607d4               ; cerror_nocancel
    0x2105a7114 <+24>: mov    sp, x29
    0x2105a7118 <+28>: ldp    x29, x30, [sp], #0x10
    0x2105a711c <+32>: ret 

有什么建议可以解决吗?还有其他更好的方法吗? 感谢您的时间和精力。带有代码示例的答案倍受赞赏。

1 个答案:

答案 0 :(得分:0)

final Collector<Integer, List<TopTerminalDto>, List<TopTerminalDto>> terminalsCollector =
             Collector.of(
                ArrayList::new,
                (terminals, p) -> terminals.add(toDto(p)),
                (accumulator, terminals) -> {
                   accumulator.addAll(terminals);
                   return accumulator;
                }
             )

final Map<Integer, List<TopTerminalDto>> map = 
             list.stream()
                 .filter(p -> p.getTerminal() != null)
                 .collect(groupingBy(p -> p.getTerminal(), terminalsCollector));

或更简单地说:

let userInfo = try managedObjectContext.fetch(request as! NSFetchRequest<NSFetchRequestResult>)

因为您已经将let userInfo = try managedObjectContext.fetch(request) 定义为

request

let request: NSFetchRequest<UserData> = UserData.fetchRequest() 始终作为userInfo的数组返回,因为请求可能返回多个对象(或不返回)。您应该测试该数组至少包含一个条目,并使用UserData或最能满足您需要的东西。显然,您可以使用userInfo.first来过滤返回的收益。