获取请求返回{(// value)}而不是仅返回值

时间:2016-10-02 20:21:41

标签: ios swift xcode core-data relationship

我很难弄清楚如何在实体之间提取关系值。我有一个Food实体和一个Meal实体。出于演示目的,我只为每个列出了1个属性。用餐的标题和食物的蛋白质。我的关系是膳食和食物,反对关系,很多甚至很多。我想要做的是,取一顿饭并返回与该膳食相关的蛋白质值。我将最终通过日期提取膳食,然后从该膳食中提取营养信息。我发现的解决方案是返回的不仅仅是模拟器标签图片所示的值。请任何帮助将非常感谢!我有一种感觉,有一种更简单的方法来做我想做的事情,但我一直在寻找几个小时而且无法弄明白。

我尝试发布图片,但它不会让我这样我会发布我的代码以及发生了什么。

ViewController.swift

import UIKit
import CoreData

class ViewController: UIViewController {
    @IBOutlet weak var label: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext

    //================================
    //CREATE MEAL AND FOOD
    //================================
    let newMeal = NSEntityDescription.insertNewObject(forEntityName: "Meal", into: context) as! Meal
    newMeal.title = "Meal 1"

    let newFood = NSEntityDescription.insertNewObject(forEntityName: "Food", into: context) as! Food
    newFood.protein = "20"
    newFood.setValue(newMeal, forKey: "meal")

    //================================
    //FETCH REQUEST
    //================================
    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Meal")
    let sortDescriptor = NSSortDescriptor(key: "title", ascending: true)
    request.sortDescriptors = [sortDescriptor]
    request.returnsObjectsAsFaults = false
    do {
        //===============================
        //EXTRACT RELATIONSHIP DATA
        //==============================
        let results = try context.fetch(request) as NSArray
        for res in results {
            let data = res as! Meal
            let item = data.food
            let strs = item.value(forKey: "protein")
            self.label.text = "\(strs)"
        }
    } catch {
        print ("error")
    }   
}

我的标签显示了这个......

{(    20 )}

我需要简单地拉出'20'。尝试将label.text值设置为str为!字符串也不会工作。任何指导?非常感谢你

2 个答案:

答案 0 :(得分:0)

以下是否会这样做?

self.label.text = strs

答案 1 :(得分:0)

我想出来了.....

import UIKit
import CoreData

class ViewController: UIViewController {

var item:NSSet = []
var strs:NSSet = []

@IBOutlet weak var label: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext

    //================================
    //CREATE MEAL AND FOOD
    //================================
    let newMeal = NSEntityDescription.insertNewObject(forEntityName: "Meal", into: context) as! Meal
    newMeal.title = "Meal 1"

    let newFood = NSEntityDescription.insertNewObject(forEntityName: "Food", into: context) as! Food
    newFood.protein = "20"
    newFood.setValue(newMeal, forKey: "meal")

    //================================
    //FETCH REQUEST
    //================================
    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Meal")
    let sortDescriptor = NSSortDescriptor(key: "title", ascending: true)
    request.sortDescriptors = [sortDescriptor]
    request.returnsObjectsAsFaults = false
    do {
        //===============================
        //EXTRACT RELATIONSHIP DATA
        //==============================
        let results = try context.fetch(request) as! [Meal]
        for res in results {

            item = res.food

            strs = item.value(forKey: "protein") as! NSSet
            let array = strs.allObjects
            print(array[0])

            self.label.text = String(format: "%@", array[0] as! CVarArg)
        }
    } catch {
        print ("error")
    }   
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

感谢您的帮助