Reputation: 768
i need to insert the days of the week Mon -> Sunday into core data, because you cant set data into core data from interface builder im doing this when the app first opens like this...
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")
}
Im storing the dayName and the dayNumber (eg, Wednesday is 2) and also adding wheather or not the day is a holiday or not. By default its set to Saturday and sunday (This can be changed by the user later)
Next in my view did load method im trying to load my data, this is where im getting stuck i think...
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")
}
}
The actually representing the data in a table view i dont think is the problem, but heres the code anyway.
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
}
For some reason all this code is only loading 1 thing from core data, and that is Sunday. Help me ;P thanks people in advance
Upvotes: 0
Views: 417
Reputation: 15589
You do one insertNewObjectForEntityForName
and change the properties of this object for each day. The solution is to do insertNewObjectForEntityForName
for each day.
Upvotes: 1
Reputation: 5554
If the question here is really about CoreData, then I'm sure there will be another answer very soon.
If the real requirement is about accessing the days of the week, there is a simpler approach
let dateFormatter = NSDateFormatter()
// dateFormatter.weekdaySymbols = "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
for dayIndex in days
{
print(dateFormatter.weekdaySymbols[dayIndex])
}
Upvotes: 0