NSCalendarDayChanged is posting without the day changing
应用程序委托中的以下代码应该每天一次向 assignments 数组添加一个新分配。问题是,每次打开应用程序时都会添加一个新任务,即使日期没有改变。似乎每次打开应用程序时 NSCalendarDayChanged 都会发布。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | var assignments = [Assignment]() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { if let _ = loadHW(){ assignments = loadHW()! } if let assignment = NSKeyedUnarchiver.unarchiveObject(withFile: Assignment.ArchiveURL.path) as? [Assignment]{ assignments = assignment } // Define identifier let notificationName = Notification.Name("NSNotification.Name.NSCalendarDayChanged") // Register to receive notification NotificationCenter.default.addObserver(self, selector: #selector(calendarDayDidChange(notification:)), name: notificationName, object: nil) // Post notification NotificationCenter.default.post(name: notificationName, object: nil) // Override point for customization after application launch.NSNotification.Name.NSCalendarDayChanged return true } func calendarDayDidChange(notification : NSNotification) { let currentDate = Date() let dateFormatter = DateFormatter() let dayOfTheWeek = Calendar(identifier: Calendar.Identifier.gregorian).component(.weekday, from: Date()) print(dayOfTheWeek) dateFormatter.dateFormat ="M/dd/YY" let convertedCurrentDate = dateFormatter.string(from: currentDate) print(convertedCurrentDate) //Adds an assignment based on date comparisons if let dates = NSKeyedUnarchiver.unarchiveObject(withFile: SettingsViewController.datesURL.path) as? [Date] { print(1) if currentDate >= dates[0] && currentDate <= dates[1] { print(2) if let weekendHW = NSKeyedUnarchiver.unarchiveObject(withFile: SettingsViewController.weekendURL.path) as? Bool { print(3) if(weekendHW || (!weekendHW && dayOfTheWeek != 1 && dayOfTheWeek != 7)){ print(4) if assignments.count > 0{ assignments.insert(Assignment(hw: assignments[0].hw, date: convertedCurrentDate)!, at: 0) print(5) }else if assignments.count == 0 { assignments.append(Assignment(hw:"", date: convertedCurrentDate)!) print(6) } saveHW() print("save complete") } } } } //adds assignment on first launch of app if assignments.count == 0 { assignments.append(Assignment(hw:"", date: convertedCurrentDate)!) saveHW() } } func saveHW() { let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(assignments, toFile: Assignment.ArchiveURL.path) //saves array grades to file defined in Assignment class if !isSuccessfulSave { //Diagnostic print("Save failed") } } func loadHW() -> [Assignment]? { return NSKeyedUnarchiver.unarchiveObject(withFile: Assignment.ArchiveURL.path) as? [Assignment] //gets array grades from file where saved, defined in Assignment class } |
我可以进行哪些更改以确保每天只创建一个作业?
不是 NSCalendarDayChanged 正在发布,您正在发布通知。删除行
1 | NotificationCenter.default.post(name: notificationName, object: nil) |
也删除
1 2 3 | if let _ = loadHW(){ assignments = loadHW()! } |
这是多余的。以下