2017-02-09 66 views
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獲取數據?謝謝...

+0

我沒有看到任何地方你正在做任何形式的抓取或保存到你的持久存儲。我的意思是你顯示了一個函數來獲取'managedObjectContext',但你甚至從來沒有根據你顯示的內容來使用它。您的永久存儲不能神奇地確定要保存和獲取的內容。另外,對於你的'mondayDayPlan',你聲明它是一個可選的'Workout'對象,但是你永遠不會初始化它......然後你將'DayPlan'對象的每個屬性設置爲'mondayDayPlan'的某個屬性,但是'mondayDayPlan '是'nil' – Pierce

+0

您需要從核心數據中獲取對象。我發現你只是使用類初始化來創建一個新的對象。 –

回答

0

聽起來好像你沒有正確地保存數據到核心數據。 您可以通過打開容易檢查: 〜/庫/應用程序支持/ iPhone模擬器/用戶/應用/

排序「修改日期」來發現你剛剛構建的應用程序。找到.sqlite文件。 檢查應該存在的數據是否真的存在。

確保在每次將NSManagedObject添加到核心數據後寫入「appDelegate.saveContext()」。

祝你好運!