核心数据 - 首次打开时创建初始数据,然后将其加载到NSTableView - OSX中

时间:2016-03-23 13:14:43

标签: swift macos core-data nstableview nsmanagedobject

我需要插入一周的日子Mon - >星期天进入核心数据,因为你无法将数据设置为接口构建器的核心数据,当应用程序首次打开时,这样做...

let firstLaunchCheck = NSUserDefaults(suiteName: "nonya-buisness ;)")

    if firstLaunchCheck?.boolForKey("firstLaunch") == false {
        print("First Launch")


        let appDel = NSApplication.sharedApplication().delegate as! AppDelegate
        let context = appDel.managedObjectContext

        let addDay =  NSEntityDescription.insertNewObjectForEntityForName("Weekdays", inManagedObjectContext: context)

        let dayIndex = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

        var dayNumber = 0

        for day in dayIndex {

            print(day)

            addDay.setValue(day, forKey: "dayName")
            addDay.setValue(dayNumber, forKey: "dayNumber")

            if dayNumber >= 5 {
                addDay.setValue(true, forKey: "dayHoliday")
            } else {
                addDay.setValue(false, forKey: "dayHoliday")
            }

            do {
                try context.save()
            } catch let error as NSError  {
                print("Could not save \(error), \(error.userInfo)")
            }

            dayNumber++
        }

        firstLaunchCheck?.setBool(true, forKey: "firstLaunch")
        firstLaunchCheck?.synchronize()
    } else {
        print("App has already been opened")
    }

我存储dayName和dayNumber(例如,星期三是2)并且还添加天气是否是假日。默认设置为星期六和星期日(以后可以由用户更改)

接下来在我的视图中确实加载方法我试图加载我的数据,这是我陷入困境我认为......

var TimetableData = [NSManagedObject]()

super.viewDidLoad() {
let appDel = NSApplication.sharedApplication().delegate as! AppDelegate
    let context = appDel.managedObjectContext
    let request = NSFetchRequest(entityName: "Weekdays")
    request.sortDescriptors = [NSSortDescriptor(key: "dayNumber", ascending: false)]

    do {
        let results = try context.executeFetchRequest(request)
        TimetableData = results as! [NSManagedObject]
        print(TimetableData.count)
        DayTableView.reloadData()

    } catch {
        print("There was a problem retrieving saved data")
}
}

实际上在表视图中表示数据我不认为是问题,但无论如何都是代码。

if tableView == DayTableView {

        let savedData = TimetableData[row]

            let cell : NextDayCell = DayTableView.makeViewWithIdentifier("nextDayCell", owner: self) as! NextDayCell





            if savedData.valueForKey("dayHoliday") as? Bool == true {
                cell.cellDayString.stringValue = savedData.valueForKey("dayName") as! String + " Weekday"
            } else {
                cell.cellDayString.stringValue = savedData.valueForKey("dayName") as! String
            }
            cell.dayNumberS.stringValue = "\(day)"

            day++

            return cell

}

出于某种原因,所有这些代码只从核心数据中加载1个东西,那就是星期日。帮助我; P提前感谢人们

2 个答案:

答案 0 :(得分:1)

您执行一次insertNewObjectForEntityForName并更改此对象的属性。解决方案是每天insertNewObjectForEntityForName

答案 1 :(得分:0)

如果这里的问题真的与CoreData有关,那么我相信很快就会有另一个答案。

如果真正的要求是关于访问一周中的几天,那么有一种更简单的方法

let dateFormatter = NSDateFormatter() 
// dateFormatter.weekdaySymbols = "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

for dayIndex in days
{    
    print(dateFormatter.weekdaySymbols[dayIndex])
}