我正在尝试从Magical Records获取最新添加的数据。我有一个名为weighttrack的实体,它有两个属性 - 重量和日期,我想获取最新添加的日期权重到我的标签,所以怎么可能呢?
class WTStatusVC: UIViewController {
var WeightArray = [WeightTracker]()
var tsk: WeightTracker?
@IBOutlet weak var currentWeightLbl: UILabel!
@IBOutlet weak var previousWeightLbl: UILabel!
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)
}
}
你可以使用Filter做到这一点
self.WeightArray = self.WeightArray.filter({ (item) -> Bool in
return item.date == tsk?.date
})