iOS9 activityManager.startActivityUpdatesToQueue 错误
iOS9 error on activityManager.startActivityUpdatesToQueue
我在新的 Xcode7 测试版中打开了我的应用程序,它在旧版本中运行良好。现在我收到错误,我不知道如何解决。这是代码。错误已被注释掉。
import UIKit
import CoreMotion
class ViewController: UIViewController {
let activityManager = CMMotionActivityManager()
let pedoMeter = CMPedometer()
@IBOutlet weak var activityState: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if(CMMotionActivityManager.isActivityAvailable()){
print("YESS!")
self.activityManager.startActivityUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: {(data: CMMotionActivity!) -> Void in //Cannot invoke 'startActivityUpdatesToQueue' with an argument list of type '(NSOperationQueue, withHandler: (CMMotionActivity!) -> Void)'
dispatch_async(dispatch_get_main_queue(), {() -> Void in
if(data.stationary == true){
self.activityState.text = "Stationary"
} else if (data.walking == true){
self.activityState.text = "Walking"
} else if (data.running == true){
self.activityState.text = "Running"
} else if (data.automotive == true){
self.activityState.text = "Automotive"
}
})
})
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
问题是数据类型CMMotionActivity?不是 CMMotionActivity!
以下是适合您的内容:
if(CMMotionActivityManager.isActivityAvailable()){
print("YES!")
self.activityManager.startActivityUpdatesToQueue(NSOperationQueue.mainQueue()) { data in
if let data = data {
dispatch_async(dispatch_get_main_queue()) {
if(data.stationary == true){
self.activityState.text = "Stationary"
} else if (data.walking == true){
self.activityState.text = "Walking"
} else if (data.running == true){
self.activityState.text = "Running"
} else if (data.automotive == true){
self.activityState.text = "Automotive"
}
}
}
}
}
也尽量利用最后一个参数是一个闭包来简化函数调用。
我在新的 Xcode7 测试版中打开了我的应用程序,它在旧版本中运行良好。现在我收到错误,我不知道如何解决。这是代码。错误已被注释掉。
import UIKit
import CoreMotion
class ViewController: UIViewController {
let activityManager = CMMotionActivityManager()
let pedoMeter = CMPedometer()
@IBOutlet weak var activityState: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if(CMMotionActivityManager.isActivityAvailable()){
print("YESS!")
self.activityManager.startActivityUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: {(data: CMMotionActivity!) -> Void in //Cannot invoke 'startActivityUpdatesToQueue' with an argument list of type '(NSOperationQueue, withHandler: (CMMotionActivity!) -> Void)'
dispatch_async(dispatch_get_main_queue(), {() -> Void in
if(data.stationary == true){
self.activityState.text = "Stationary"
} else if (data.walking == true){
self.activityState.text = "Walking"
} else if (data.running == true){
self.activityState.text = "Running"
} else if (data.automotive == true){
self.activityState.text = "Automotive"
}
})
})
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
问题是数据类型CMMotionActivity?不是 CMMotionActivity!
以下是适合您的内容:
if(CMMotionActivityManager.isActivityAvailable()){
print("YES!")
self.activityManager.startActivityUpdatesToQueue(NSOperationQueue.mainQueue()) { data in
if let data = data {
dispatch_async(dispatch_get_main_queue()) {
if(data.stationary == true){
self.activityState.text = "Stationary"
} else if (data.walking == true){
self.activityState.text = "Walking"
} else if (data.running == true){
self.activityState.text = "Running"
} else if (data.automotive == true){
self.activityState.text = "Automotive"
}
}
}
}
}
也尽量利用最后一个参数是一个闭包来简化函数调用。