В настоящее время у меня есть два разных класса, Alarm и AlarmMO, AlarmMO является подклассом NSManagedObject.Я пытаюсь объединить эти два в один класс объекта, если это возможно, и у меня возникли некоторые проблемы.Как лучше всего это сделать?
Это то, что у меня сейчас, и я получаю всевозможные ошибки:
class Alarm: NSManagedObject {
//MARK: Properties
@NSManaged var alarmTime: Double
@NSManaged var alarmNumber: Int
@NSManaged var startTimeInterval: Double
@NSManaged var endTimeInterval: Double
@NSManaged var note: String
@NSManaged var notificationUuids: [String]
@NSManaged var recurrenceIndex: Int
var recurrence: RecurrenceOptions = .today
let NUMBER_OF_ALLOWED_NOTIFICATIONS_CREATED_AT_ONE_TIME = 10
}
extension Alarm {
convenience init?(alarmNumber: Int, timeIntervals: TimeIntervals, note: String, recurrence: RecurrenceOptions) {
self.alarmNumber = alarmNumber
self.note = note
self.recurrence = recurrence
self.notificationUuids = [String]()
let date = Date()
let calendar = Calendar.current
let currentDateComponents = calendar.dateComponents([.year, .month, .day, .timeZone, .hour, .minute], from: date)
var dateComponents = DateComponents()
dateComponents.year = currentDateComponents.year
dateComponents.month = currentDateComponents.month
dateComponents.timeZone = currentDateComponents.timeZone
dateComponents.day = currentDateComponents.day
let startInterval = Alarm.convertToTimeDouble(hour: timeIntervals.hourStartInterval, minute: timeIntervals.minuteStartInterval)
let endInterval = Alarm.convertToTimeDouble(hour: timeIntervals.hourEndInterval, minute: timeIntervals.minuteEndInterval)
self.startTimeInterval = startInterval
self.endTimeInterval = endInterval
if endInterval < startInterval {
os_log("Error: Alarm time endInterval is before startInterval", log: OSLog.default, type: .info)
return nil
}
self.alarmTime = Double.random(in: startInterval ... endInterval)
let hour = Alarm.extractHourFromTimeDouble(alarmTimeDouble: self.alarmTime)
let minute = Alarm.extractMinuteFromTimeDouble(alarmTimeDouble: self.alarmTime)
os_log("Attempting to create alarm with time %d:%02d", log: OSLog.default, type: .info, hour, minute)
createNotifications(dateComponents: dateComponents)
}
public static func convertToTimeDouble(hour: Int, minute: Int) -> Double {
return Double(hour) + (Double(minute) / 60.0)
}
public static func extractHourFromTimeDouble(alarmTimeDouble: Double) -> Int {
return Int(floor(alarmTimeDouble))
}
public static func extractMinuteFromTimeDouble(alarmTimeDouble: Double) -> Int {
return Int(round((alarmTimeDouble - floor(alarmTimeDouble)) * 60))
}
func createNotifications(dateComponents: DateComponents) {
switch (self.recurrence) {
case .today:
self.createNotification(for: dateComponents)
case .tomorrow:
self.createNotification(for: self.day(after: dateComponents))
case .daily:
var numberOfCreatedNotifications = 0
var currentDay: DateComponents? = dateComponents
while numberOfCreatedNotifications < self.NUMBER_OF_ALLOWED_NOTIFICATIONS_CREATED_AT_ONE_TIME {
self.createNotification(for: currentDay)
currentDay = self.day(after: currentDay)
numberOfCreatedNotifications += 1
}
}
}
//MARK: Private functions
private func createNotification(for dateComponents: DateComponents?) {
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "Random Alarm"
content.subtitle = "It's time!"
content.body = self.note
content.sound = UNNotificationSound.default
guard let dateComponents = dateComponents else {
os_log("Could not unwrap dateComponents in createNotification() in Alarm.swift", log: OSLog.default, type: .debug)
return
}
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString, content: content, trigger: trigger)
self.notificationUuids.append(uuidString)
guard let day = dateComponents.day else {
os_log("Could not unwrap dateComponents.day in createNotification() in Alarm.swift", log: OSLog.default, type: .debug)
return
}
guard let hour = dateComponents.hour else {
os_log("Could not unwrap dateComponents.hour in createNotification() in Alarm.swift", log: OSLog.default, type: .debug)
return
}
guard let minute = dateComponents.minute else {
os_log("Could not unwrap dateComponents.minute in createNotification() in Alarm.swift", log: OSLog.default, type: .debug)
return
}
os_log("Creating notification for day: %d, time: %d:%02d, with uuid=%s", log: OSLog.default, type: .debug, day, hour, minute, uuidString)
center.add(request) { (error) in
if let err = error {
print("error \(err.localizedDescription)")
}
}
}
private func day(after dateComponents: DateComponents?) -> DateComponents? {
let calendar = Calendar.autoupdatingCurrent
guard let dateComponents = dateComponents,
let date = calendar.date(from: dateComponents),
let tomorrow = calendar.date(byAdding: .day, value: 1, to: date)
else {
os_log("Could not calculate tomorrow in Alarm.swift", log: OSLog.default, type: .debug)
return nil
}
let newDateComponents = calendar.dateComponents([.year, .month, .day, .timeZone, .hour, .minute], from: tomorrow)
return newDateComponents
}
}