できていること
Swift
1import UIKit 2import UserNotifications 3 4class ViewController: UIViewController { 5 6 @IBAction func action(_ sender: Any) { 7 8 let content = UNMutableNotificationContent() 9 content.title = "通知" 10 content.subtitle = "テスト" 11 content.body = "これはボディーです。" 12 content.badge = 1 13 content.sound = .default() 14 15 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) 16 let request = UNNotificationRequest(identifier: "あ", content: content, trigger: trigger) 17 18 UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 19 20 } 21 22 23 override func viewDidLoad() { 24 super.viewDidLoad() 25 // Do any additional setup after loading the view, typically from a nib. 26 27 UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in}) 28 } 29 30 override func didReceiveMemoryWarning() { 31 super.didReceiveMemoryWarning() 32 // Dispose of any resources that can be recreated. 33 } 34 35 36} 37
↑↑このプログラムではボタンを押すと5秒後に通知が来ます。
やろうとしていること
Swift
1class ViewController: UIViewController { 2 3 @IBAction func action(_ sender: Any) { 4 5 let ss : [Double] = [10.0] 6 7 let content = UNMutableNotificationContent() 8 content.title = "通知" 9 content.subtitle = "テスト" 10 content.body = "これはボディーです。" 11 content.badge = 1 12 content.sound = .default() 13 14 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: ss, repeats: false) 15 let request = UNNotificationRequest(identifier: "あ", content: content, trigger: trigger) 16 17 UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 18 19 } 20 21 22 override func viewDidLoad() { 23 super.viewDidLoad() 24 // Do any additional setup after loading the view, typically from a nib. 25 26 UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in}) 27 } 28 29 override func didReceiveMemoryWarning() { 30 super.didReceiveMemoryWarning() 31 // Dispose of any resources that can be recreated. 32 } 33 34 35} 36
↑↑5秒後のところを定数にするとエラーが起きます。
変数や定数で秒数を指定することはできないんでしょうか?
これでもエラーが出ました。
ですが、下のコードは定数を配列にしなかったらエラーが消えました。
ユーザーデフォルトを使っていて配列の最後のInt型をtimeIntervalに入れたいのですが、できますか?
よろしくお願いします。
Swift
1class ViewController: UIViewController { 2 3 @IBAction func action(_ sender: Any) { 4 5 let ss = 10.0 6 7 let content = UNMutableNotificationContent() 8 content.title = "通知" 9 content.subtitle = "テスト" 10 content.body = "これはボディーです。" 11 content.badge = 1 12 content.sound = .default() 13 14 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: ss, repeats: false) 15 let request = UNNotificationRequest(identifier: "あ", content: content, trigger: trigger) 16 17 UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 18 19 } 20 21 22 override func viewDidLoad() { 23 super.viewDidLoad() 24 // Do any additional setup after loading the view, typically from a nib. 25 26 UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in}) 27 } 28 29 override func didReceiveMemoryWarning() { 30 super.didReceiveMemoryWarning() 31 // Dispose of any resources that can be recreated. 32 } 33 34 35}
初心者でわからないのでよろしくお願いします。
エラー
Cannot convert value of type '[Double]' to expected argument type 'TimeInterval' (aka 'Double')
補足
Swift4.0
回答2件
あなたの回答
tips
プレビュー