我的下面代码是否可以显示所有练习,而不仅仅是当天附加的练习。现在,如果我使用day.dayExercises并使用for循环,我可以访问tableview所需的所有必要数据,但是我不确定如何更改fetchrequest以在day.dayexercises关系中使用对象。
class ExercisesTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {
var dayExercises:[Exercises] = []
var day:Days!
var fetchResultController:NSFetchedResultsController!
override func viewDidLoad() {
super.viewDidLoad()
self.title = "\(day.day)"
// print(day.dayExercises)
for item in day.dayExercises{
print(item)
}
let fetchRequest = NSFetchRequest(entityName: "Exercises")
let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
fetchResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
fetchResultController.delegate = self
do {
try fetchResultController.performFetch()
dayExercises = fetchResultController.fetchedObjects as! [Exercises]
} catch {
print(error)
}
}
print(item)输出以下内容:
<Gym_Notes.Exercises: 0x12e66d4d0> (entity: Exercises; id: 0xd000000000080004 <x-coredata://XXXXX-D879-XXXX-9DE7-7AXX14XXX623/Exercises/p2> ; data: {
exerciseDay = "0xd000000000040002 <x-coredata://79XX0B7D-XX79-43XX-9DE7-7A26XXXX2623/Days/p1>";
image = <89504e47 0d0a1a0a 0000000d 49484452 00000080 00000080 08060000 00c33e61 cb000000 01735247 4200aece 1ce90000 0009>;
name = Bob;
reps = Bob;
rest = Bob;
sets = Bob;
weight = Bob;
})