gpt4 book ai didi

ios - 如何将天数求和为 NSDate

转载 作者:行者123 更新时间:2023-11-30 13:23:48 25 4
gpt4 key购买 nike

如何将天数与 NSDate 类型的 DATE 相加,以便从此 DATE 创建多个事件?

这是我控制 iOS 日历的代码:

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.
}
}

最佳答案

已编辑

正如 @vadian 所建议的,我使用日历方法编辑了答案。

我建议您创建自己的 NSDate 扩展,例如:

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)!
}

}

通过这种方式,您可以:

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

关于ios - 如何将天数求和为 NSDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37457012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com