0
这里是我的代码: 进口的UIKit 进口CoreData斯威夫特CoreData到UICollectionView
class WeekController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
func getContext() -> NSManagedObjectContext {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
return appDelegate.persistentContainer.viewContext
}
var dayPlan: [DayPlan] = {
var mondayDayPlan = DayPlan()
var mondayDayPlanWorkout : Workout?
mondayDayPlan.dayNameLabel = mondayDayPlanWorkout?.mondayNameCD
mondayDayPlan.codeNameLabel = mondayDayPlanWorkout?.mondayCodeNameCD
mondayDayPlan.task01 = mondayDayPlanWorkout?.mondayTask01
mondayDayPlan.task02 = mondayDayPlanWorkout?.mondayTask02
mondayDayPlan.task03 = mondayDayPlanWorkout?.mondayTask03
mondayDayPlan.task04 = mondayDayPlanWorkout?.mondayTask04
mondayDayPlan.task05 = mondayDayPlanWorkout?.mondayTask05
mondayDayPlan.dayIconImage = #imageLiteral(resourceName: "biceps")
var tuesdayDayPlan = DayPlan()
tuesdayDayPlan.dayNameLabel = "Tuesday"
tuesdayDayPlan.codeNameLabel = "pojdme na ty nohy"
tuesdayDayPlan.task01 = "5x1 Legs"
tuesdayDayPlan.task02 = "5x2 Legs"
tuesdayDayPlan.task03 = "5x3 Legs"
tuesdayDayPlan.task04 = "5x4 Legs"
tuesdayDayPlan.task05 = "5x5 Legs"
tuesdayDayPlan.dayIconImage = #imageLiteral(resourceName: "ass")
return [mondayDayPlan, tuesdayDayPlan]
我有这个coreData型号:
Entity: **Workout**
with this Attributes:
mondayCodeNameCD
mondayFocusCD
mondayNameCD
mondayTask01
mondayTask02
mondayTask03
mondayTask04
mondayTask05
mondayTask06
more, more, more...
tuesdayDayPlan被硬编码(工作好),但mondayDayPlan是CoreData(显示nothink)...没有错误,但星期一是空的,为什么?在这种情况下如何从CoreData获取数据?谢谢...
我没有看到任何地方你正在做任何形式的抓取或保存到你的持久存储。我的意思是你显示了一个函数来获取'managedObjectContext',但你甚至从来没有根据你显示的内容来使用它。您的永久存储不能神奇地确定要保存和获取的内容。另外,对于你的'mondayDayPlan',你声明它是一个可选的'Workout'对象,但是你永远不会初始化它......然后你将'DayPlan'对象的每个属性设置为'mondayDayPlan'的某个属性,但是'mondayDayPlan '是'nil' – Pierce
您需要从核心数据中获取对象。我发现你只是使用类初始化来创建一个新的对象。 –