Ángel López Torres Ángel López Torres - 6 months ago 19
iOS Question

How to sum days to a NSDATE, in Swift

How I could sum days to a DATE of type NSDate in order to create multiple events from this DATE?

This is my code that control the IOS calendar:


import UIKit import EventKit class ViewController:
UIViewController {

}
@IBOutlet weak var needPermissionView: UIView!

let eventStore = EKEventStore()

override func viewDidLoad() {
super.viewDidLoad()
checkCalendarAuthorizationStatus()
// 1
//let eventStore : EKEventStore = EKEventStore()

/*
// 2
switch EKEventStore.authorizationStatusForEntityType(EKEntityType.Event) {
case .Authorized:
print("Authorized")
// insertEvent(eventStore)
case .Denied:
print("Access denied")
case .NotDetermined:
break
// 3
default:
print("Case Default")
} // insertEvent(eventStore) */
}




func checkCalendarAuthorizationStatus() {
let status = EKEventStore.authorizationStatusForEntityType(EKEntityType.Event)

switch (status) {
case EKAuthorizationStatus.NotDetermined:
// This happens on first-run
requestAccessToCalendar()
case EKAuthorizationStatus.Authorized:
// Things are in line with being able to show the calendars in the table view
loadCalendars()
// refreshTableView()
case EKAuthorizationStatus.Restricted, EKAuthorizationStatus.Denied:
// We need to help them give us permission
needPermissionView.fadeIn()
}
}

func requestAccessToCalendar() {

eventStore.requestAccessToEntityType(EKEntityType.Event, completion: {
(accessGranted: Bool, error: NSError?) in

if accessGranted == true {
dispatch_async(dispatch_get_main_queue(), {
self.loadCalendars() // self.loadCalendars() // self.refreshTableView()
})
} else {
dispatch_async(dispatch_get_main_queue(), {
self.needPermissionView.fadeIn()
})
}
})
}



func insertEvent(store: EKEventStore) {
// 1
let calendars = store.calendarsForEntityType(EKEntityType.Event)


for calendar in calendars {
// 2
if calendar.title == "ioscreator" {
// 3
let startDate = NSDate()
// 2 hours
let endDate = startDate.dateByAddingTimeInterval(2 * 60 * 60)

// 4
// Create Event
let event = EKEvent(eventStore: store)
event.calendar = calendar

event.title = "New Meeting"
event.startDate = startDate
event.endDate = endDate

// 5
// Save Event in Calendar
var error: NSError?
let result: Bool
do {

try store.saveEvent(event, span: .ThisEvent)
result = true
} catch let error1 as NSError {
error = error1
result = false
}

if result == false {
if let theError = error {
print("An error occured \(theError)")
}
}
}
}
}

func loadCalendars() {
insertEvent(eventStore) // self.calendars = eventStore.calendarsForEntityType(EKEntityType.Event)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}


Answer

Edited

As @vadian suggests, I edited answer using calendar methods.

I suggest you to create your own NSDate extension, for instance:

public extension NSDate {

   func addDays(days: Int) -> NSDate {
       return self.addUnit(.Day, value: days)
   }

   func addHours(hours: Int) -> NSDate {
       return self.addUnit(.Hour, value: hours)
   }

   func addUnit(unit: NSCalendarUnit, value: Int) -> NSDate {
       let calendar = NSCalendar.currentCalendar()
       return calendar.dateByAddingUnit(unit, value: value, toDate: self, options: .MatchNextTime)!
   }

}

In this way you can:

let date = NSDate()
let.addHours(10)