gpt4 book ai didi

activityManager.startActivityUpdatesToQueue 上的 iOS9 错误

转载 作者:可可西里 更新时间:2023-11-01 00:52:56 24 4
gpt4 key购买 nike

我在新的 Xcode7 beta 中打开了我的应用程序,它在旧版本中运行良好。现在我收到错误,我不知道如何解决。这是代码。错误被注释掉。

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"
}
}
}
}
}

同时尝试利用最后一个参数是一个闭包来简化函数调用。

关于activityManager.startActivityUpdatesToQueue 上的 iOS9 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30825409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com