NSTimer 错误(Swift:Xcode)
Error in NSTimer (Swift: Xcode)
我正在制作一个计时器,它将重复调用一个函数 swift 2,Xcode 7。此函数发送本地推送通知。我的计时器变量在 ViewController.swift 中的 viewDidLoad() 之外声明。
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self , selector: Selector("notification"), userInfo: nil, repeats: true)
我收到一个错误:
参数类型 'NSObject -> () -> ViewController 不符合预期类型 'AnyObject'
目标自身似乎有问题,因为它以红色突出显示。
如果需要,这是我的完整代码:
import UIKit
class ViewController: UIViewController {
var time = 10
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self , selector: Selector("notification"), userInfo: nil, repeats: true)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func btnPressed(sender: AnyObject) {
let alertView = UIAlertController(title: "You won!", message: "Press Go to continue!", preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alertView, animated: true, completion: nil)
alertView.addAction(UIAlertAction(title: "Go", style: UIAlertActionStyle.Default, handler: nil))
}
func notification(){
time--
if (time == 0){
let notification = UILocalNotification()
notification.alertAction = "Go back to App!"
notification.alertBody = "Pls Go back to my App!"
notification.fireDate = NSDate(timeIntervalSinceNow: 0)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
timer.invalidate()
}
}
}
提前致谢:)
尝试声明 var timer only setting as NSTimer.init() like:
var timer = NSTimer.init()
然后,将 scheduledTimerWithInterval 的初始化放在 viewDidLoad():
override func viewDidLoad() {
super.viewDidLoad()
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "notification", userInfo: nil, repeats: true)
}
我正在制作一个计时器,它将重复调用一个函数 swift 2,Xcode 7。此函数发送本地推送通知。我的计时器变量在 ViewController.swift 中的 viewDidLoad() 之外声明。
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self , selector: Selector("notification"), userInfo: nil, repeats: true)
我收到一个错误:
参数类型 'NSObject -> () -> ViewController 不符合预期类型 'AnyObject'
目标自身似乎有问题,因为它以红色突出显示。
如果需要,这是我的完整代码:
import UIKit
class ViewController: UIViewController {
var time = 10
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self , selector: Selector("notification"), userInfo: nil, repeats: true)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func btnPressed(sender: AnyObject) {
let alertView = UIAlertController(title: "You won!", message: "Press Go to continue!", preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alertView, animated: true, completion: nil)
alertView.addAction(UIAlertAction(title: "Go", style: UIAlertActionStyle.Default, handler: nil))
}
func notification(){
time--
if (time == 0){
let notification = UILocalNotification()
notification.alertAction = "Go back to App!"
notification.alertBody = "Pls Go back to my App!"
notification.fireDate = NSDate(timeIntervalSinceNow: 0)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
timer.invalidate()
}
}
}
提前致谢:)
尝试声明 var timer only setting as NSTimer.init() like:
var timer = NSTimer.init()
然后,将 scheduledTimerWithInterval 的初始化放在 viewDidLoad():
override func viewDidLoad() {
super.viewDidLoad()
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "notification", userInfo: nil, repeats: true)
}