核心数据 - 在首次打开时创建初始数据,然后将其加载到 NSTableView - OSX
Core Data - Create Initial Data on First Open, Then Load it into NSTableView - OSX
我需要将星期几插入到核心数据中,周一 -> 周日,因为你无法将数据设置到核心数据中,因为当应用程序首次打开时,我正在这样做...
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")
}
}
在 table 视图中实际表示数据我不认为是问题所在,但无论如何这是代码。
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 提前感谢大家
如果这里的问题真的是关于 CoreData 的,那么我相信很快就会有另一个答案。
如果真正的需求是关于访问星期几,有一个更简单的方法
let dateFormatter = NSDateFormatter()
// dateFormatter.weekdaySymbols = "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
for dayIndex in days
{
print(dateFormatter.weekdaySymbols[dayIndex])
}
你做一个 insertNewObjectForEntityForName
并每天更改此对象的属性。解决方案是每天做 insertNewObjectForEntityForName
。
我需要将星期几插入到核心数据中,周一 -> 周日,因为你无法将数据设置到核心数据中,因为当应用程序首次打开时,我正在这样做...
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")
}
}
在 table 视图中实际表示数据我不认为是问题所在,但无论如何这是代码。
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 提前感谢大家
如果这里的问题真的是关于 CoreData 的,那么我相信很快就会有另一个答案。
如果真正的需求是关于访问星期几,有一个更简单的方法
let dateFormatter = NSDateFormatter()
// dateFormatter.weekdaySymbols = "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
for dayIndex in days
{
print(dateFormatter.weekdaySymbols[dayIndex])
}
你做一个 insertNewObjectForEntityForName
并每天更改此对象的属性。解决方案是每天做 insertNewObjectForEntityForName
。