2016-10-14 22 views
0

我在一個月和一段時間內存儲了一天。我正在創建一個通知,我需要做一點計算然後創建一個日期。我堅持將所有部分「粘合」到Date的部分,以便我可以創建fireDate。如何從Swift 3中的組件創建日期

我真的不知道這是如何工作的,雖然我試圖找出使用Apple文檔。當我收集我需要DateComponents但我不知道如何繼續。只有一個init()方法需要這麼多參數。

現在我已經很少代碼:

let calendar = Calendar(identifier: .gregorian) 
let components = DateComponents() 
calendar.date(from: components) 

有人可以請澄清這對我? 謝謝你一堆。

+0

什麼是你輸入你怎麼想作爲輸出? –

+0

因此,我在一個月內有'Int',並且我有來自日期選擇器的數據(我用它來獲得時間)。我需要從這些單獨的數據創建一個日期。我不知道我該如何實現這一目標。 – Kira

+0

你可以發佈你從日期選擇器獲得的輸出嗎?只有一天,Int不能幫你創建一個日期 –

回答

1

這聽起來像你想創建一個通知,在特定的日期被解僱。您需要從DateComponents創建UNCalendarNotificationTrigger。所以給定一個日期,您需要從該日期創建一個DateComponents對象。

// Assuming your UIDatePicker is named `datePicker` 
let triggerDate = datePicker.date 
let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: triggerDate) 
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false) 

下面是完整的代碼,你需要在某個日期以觸發通知:

let content = UNMutableNotificationContent() 
content.title = "Notification Title" 
let triggerDate = datePicker.date 
let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: triggerDate) 
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false) 
let request = UNNotificationRequest(identifier: nil, content: content, trigger: trigger) 
UNUserNotificationCenter.current().add(request) 
+0

這個答案真的有幫助!我只用了我的'Int'的day組件 – Kira

相關問題