2016-09-21 7 views
1

私は、通知を作り、その上にチュートリアルを見て、私はタイプされたときにされた:Swift3のtimeIntervalSinceNowの操作方法は?

notification.fireDate = NSDate(timeIntervalSinceNow: 0) 

それは

引数ラベル「(timeIntervalSinceNowが:)」は任意の利用可能 オーバーロードと一致していないと言う

私はこれをどのように修正すればよいですか?ここに私のコードは次のとおりです。あなたが火の日は今になりたい場合は

import UIKit 
import UserNotifications 

class ViewController: UIViewController { 
    var timer = Timer() 
    var time = 10 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: Selector(("Notification")), userInfo: nil, repeats: true) 
     // Do any additional setup after loading the view, typically from a nib. 
    } 
    func notification() { 
     time -= 1 
     if time <= 0 { 
      let notification = UILocalNotification() 
      notification.alertAction = "Call" 
      notification.alertBody = "You have a call right now" 
      notification.fireDate = NSDate(timeIntervalSinceNow: 0) 
      UIApplication.shared.scheduleLocalNotification(notification) 
      timer.invalidate() 
     } 
    } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
    @IBAction func pushNotification(_ sender: AnyObject) { 
     let AlertView = UIAlertController(title: "Time for your call!", message: "Press go to continue", preferredStyle: .alert) 
     AlertView.addAction(UIAlertAction(title: "Go", style: .default, handler: nil)) 
     self.present(AlertView, animated: true, completion: nil) 
    } 
} 
+0

しかし、私はそれを 'Notification.fireDate = NSDate()としてDate'としなければならないと言います。それでうまくいきません。 –

+0

@LeoDabusあなたが言ったことをするとエラーは発生しませんが、 1:signal SIGABRT –

+0

@LeoDabusどこに置くべきですか? –

答えて

1

、それだけだ。

notification.fireDate = Date() 

レオは他の場所で述べたように、セレクタが正しくありません。

を間違いなく動作するが、私はアプリ

を離れる際に、今私は、通知を得ることはありませんあなたは得ることはありません:あなたは、その後尋ね

weak var timer: Timer? 
var time = 10 

override func viewDidLoad() { 
    super.viewDidLoad() 

    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(handleTimer(_:)), userInfo: nil, repeats: true) 
} 

func handleTimer(_ timer: Timer) { 
    time -= 1 

    if time <= 0 { 
     let notification = UILocalNotification() 

     notification.alertAction = "Call" 
     notification.alertBody = "You have a call right now" 
     notification.fireDate = Date() 

     UIApplication.shared.scheduleLocalNotification(notification) 

     timer.invalidate() 
    } 
} 

:それはする必要がありますTimerは、アプリが実行されていないときに起動し続けないので、あなたが、あなたのアプリケーションを離れる通知。タイマーを完全になくし、ただちに希望の時間のローカル通知をスケジュールするほうがよいでしょう。

override func viewDidLoad() { 
    super.viewDidLoad() 

    scheduleLocalNotification(delay: 10) 
} 

func scheduleLocalNotification(delay: TimeInterval) { 
    let notification = UILocalNotification() 

    notification.alertAction = "Call" 
    notification.alertBody = "You have a call right now" 
    notification.fireDate = Date().addingTimeInterval(delay) 

    UIApplication.shared.scheduleLocalNotification(notification) 
} 
+0

@LeoDabusは、すでに私は、私は 'スレッド1を取得していることを入力するときと言っアプリ離れるときに、今私は、通知を得ることはありません:あなたのセレクタが間違っているので、信号SIGABRT' –

+2

をはい、それは、ですがレオが指摘したように。 – Rob

1

スウィフト今まで11分間3

let minute:TimeInterval = 11.0 * 60.0; 
Date(timeIntervalSinceNow: minute); 

:たとえば、10秒で起動するようにローカル通知をスケジュールします。

関連する問題