我正在尝试从魔术记录中获取最新添加的数据

时间:2019-04-17 09:37:49

标签: swift magicalrecord

我正在尝试从魔术记录中获取最新添加的数据。我有一个称为weighttrack的实体,它有两个属性-weight和date,我想获取标签上最新添加的日期weight,那么怎么可能呢?任何帮助将不胜感激。 https://i.stack.imgur.com/LepDt.png][1]

class WTStatusVC: UIViewController {
    var WeightArray = [WeightTracker]()
    var tsk: WeightTracker?
    @IBOutlet weak var currentWeightLbl: UILabel!
    @IBOutlet weak var previousWeightLbl: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewWillAppear(_ animated: Bool) {
        super .viewWillAppear(animated)

        if let data = WeightTracker.mr_findAll(with: NSPredicate(format: "date == %@", "\(String(describing: tsk?.date))")) as? [WeightTracker]{

            WeightArray = data
            DispatchQueue.main.async {
               self.currentWeightLbl.text = self.tsk?.weight
            }
            print(data)
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用过滤器

self.WeightArray = self.WeightArray.filter({ (item) -> Bool in
               return item.date == tsk?.date   
       })