获取核心数据字符串并放置在标签中(Swift4) [英] fetch core data string and place in a label (Swift4)

查看:74
本文介绍了获取核心数据字符串并放置在标签中(Swift4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用2个不同的核心数据字符串,并将它们分别放在单独的标签上.现在,我收到错误消息:无法为类型为'([[NSManagedObject])'的参数列表调用类型为'init(_ :)'的初始化程序.此错误来自j1.text = String(itemsName).我添加了两个用于保存和显示的视图控制器.

I am trying to call 2 different core data strings and place them each on separate labels. Right now I am getting the error Cannot invoke initializer for type 'init(_:)' with an argument list of type '([NSManagedObject])'. This error is coming from j1.text = String(itemsName). I added both view controllers for saving and displaying.

        import UIKit
import CoreData

class ViewController: UIViewController {

@IBOutlet var j1 : UITextField!
@IBOutlet var j2 : UITextField!


@IBAction func save(){

    let appD = UIApplication.shared.delegate as! AppDelegate

    let context = appD.persistentContainer.viewContext
    let entity = NSEntityDescription.entity(forEntityName: "Team", in : context)!

    let theTitle = NSManagedObject(entity: entity, insertInto: context)
    theTitle.setValue(j1.text, forKey: "score")
    theTitle.setValue(j2.text, forKey: "alba")


    do {
        try context.save()
    }
    catch {
        print("Tom Corley")

    }
}}
class twoVC: UIViewController {

    @IBOutlet var j1 : UILabel!
    @IBOutlet var j2 : UILabel!

        var itemsName : [NSManagedObject] = []
      var itemsName2 : [NSManagedObject] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let appD = UIApplication.shared.delegate as! AppDelegate

        let context = appD.persistentContainer.viewContext

        let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Team")
        fetchRequest.sortDescriptors = [NSSortDescriptor(key: "score", ascending: true)]


        let fetchRequest2 = NSFetchRequest<NSManagedObject>(entityName: "Team")
        fetchRequest.sortDescriptors = [NSSortDescriptor(key: "alba", ascending: true)]





        do {
            itemsName = try context.fetch(fetchRequest)
            itemsName2 = try context.fetch(fetchRequest2)
            if let score = itemsName[0].value(forKey: "score") {
                j1.text = (score as! String)
            }
            if let alba = itemsName2[0].value(forKey: "alba") {
                j2.text = (alba as? String)
            }


        }catch {
            print("Ashley Tisdale")
        }
    }}

推荐答案

将获取的结果放在循环上,并附加到一个字符串(该字符串随后用作标签的值)中,该字符串位于do{...}内部,您可以在其中执行今天获取.请注意,我在这里仅使用一个提取请求.

Loop over the result from the fetch and append to a string that is then used as value for the label, this goes inside the do{...} where you do the fetch today. Note that I am only using one fetch request here.

itemsName = try context.fetch(fetchRequest)
var mergedScore: String = ""
var mergedAlba: String = ""
for item in itemsName {
    if let score = item.value(forKey: "score") as? String {
         mergedScore.append(score)
         mergedScore.append(" ") //separator
    }
    if let alba = item.value(forKey: "alba") as? String {
         mergedScore.append(alba)
         mergedScore.append(" ") //separator
    }
}
j1.text = mergedScore
j2.text = mergedAlba

这篇关于获取核心数据字符串并放置在标签中(Swift4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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