Swift 快速错误“;无法分配类型为';接口控制器';输入HKWorkoutSessionLegate';
我在HealthKit中的watchOS代码测量心率时出现了一条错误消息,在另一个Xcode项目中工作正常。我比较了代码,它们看起来是一样的。文件名为InterfaceController.swift 对于行'self.workoutsession?.delegate=self'我收到红旗错误“无法将'interface controller'类型的值分配给HKworkoutsessionelegate”类型有什么想法吗 下面是函数的代码。最后一段是带有错误的函数(前面的代码用于上下文)。谢谢你的帮助Swift 快速错误“;无法分配类型为';接口控制器';输入HKWorkoutSessionLegate';,swift,debugging,healthkit,Swift,Debugging,Healthkit,我在HealthKit中的watchOS代码测量心率时出现了一条错误消息,在另一个Xcode项目中工作正常。我比较了代码,它们看起来是一样的。文件名为InterfaceController.swift 对于行'self.workoutsession?.delegate=self'我收到红旗错误“无法将'interface controller'类型的值分配给HKworkoutsessionelegate”类型有什么想法吗 下面是函数的代码。最后一段是带有错误的函数(前面的代码用于上下文)。谢谢你
import WatchKit
import Foundation
import HealthKit
class InterfaceController: WKInterfaceController {
@IBOutlet var label: WKInterfaceLabel!
@IBOutlet private weak var heart: WKInterfaceImage!
@IBOutlet var deviceLabel: WKInterfaceLabel!
@IBOutlet var startStopButton: WKInterfaceButton!
let healthStore = HKHealthStore()
//State of the app - is the workout activated
var workoutActive = false
// define the activity type and location
var workoutSession : HKWorkoutSession?
let heartRateUnit = HKUnit(fromString: "count/min")
var anchor = HKQueryAnchor(fromValue: Int(HKAnchoredObjectQueryNoAnchor))
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
}
override func willActivate() {
super.willActivate()
guard HKHealthStore.isHealthDataAvailable() == true else {
label.setText("not available")
return
}
guard let quantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate) else {
displayNotAllowed()
return
}
let dataTypes = Set(arrayLiteral: quantityType)
healthStore.requestAuthorizationToShareTypes(nil, readTypes: dataTypes) { (success, error) -> Void in
if success == false {
self.displayNotAllowed()
}
}
}
func displayNotAllowed() {
label.setText("not allowed")
}
func workoutSession(workoutSession: HKWorkoutSession, didChangeToState toState: HKWorkoutSessionState, fromState: HKWorkoutSessionState, date: NSDate) {
switch toState {
case .Running:
workoutDidStart(date)
case .Ended:
workoutDidEnd(date)
default:
print("Unexpected state \(toState)")
}
}
func workoutSession(workoutSession: HKWorkoutSession, didFailWithError error: NSError) {
// Do nothing for now
NSLog("Workout error: \(error.userInfo)")
}
func workoutDidStart(date : NSDate) {
if let query = createHeartRateStreamingQuery(date) {
healthStore.executeQuery(query)
} else {
label.setText("cannot start")
}
}
func workoutDidEnd(date : NSDate) {
if let query = createHeartRateStreamingQuery(date) {
healthStore.stopQuery(query)
label.setText("---")
} else {
label.setText("cannot stop")
}
}
// MARK: - Actions
@IBAction func startBtnTapped() {
if (self.workoutActive) {
//finish the current workout
self.workoutActive = false
self.startStopButton.setTitle("Start")
if let workout = self.workoutSession {
healthStore.endWorkoutSession(workout)
}
} else {
//start a new workout
self.workoutActive = true
self.startStopButton.setTitle("Stop")
startWorkout()
}
}
func startWorkout() {
self.workoutSession = HKWorkoutSession(activityType: HKWorkoutActivityType.CrossTraining, locationType: HKWorkoutSessionLocationType.Indoor)
self.workoutSession?.delegate = self
healthStore.startWorkoutSession(self.workoutSession!)
}
self
未实现HKWorkoutSessionLegate
?您没有显示代码的这一部分
当您实现所需的方法时,您必须进行更改
class InterfaceController:WKInterfaceController{
将来
class InterfaceController:WKInterfaceController,HKWorkoutSessionLegate{
这告诉编译器该类实现了该协议。啊,你是对的。在我从它复制的另一个Xcode项目中,确实包含了我忽略的那一行。在这个Xcode项目中,它现在出现了9个其他红色警报错误:在AppDelegate.swift上,它现在显示出来了使用未声明的类型“WCSessionDelegate”,然后是“使用未解析标识符“HKHealthStore”,再加上“使用未解析标识符“self”。(我将在上面添加代码)。以及“HKWorkoutSessionDelegate不可用”和“HKWorkoutSession不可用”的6个实例在该swift文件上。我已从我的应用程序委托中删除了“WCSessionDelegate”,并添加了“var window:UIWindow?let healthStore=HKHealthStore()“在interfacecontroller文件的顶部,它已经消除了这些红色错误,只留下了一个‘使用未声明的类型’UIWindow’?我对这是一个新手,所以我想知道如何声明它?您已经回答了第一个错误/问题,但表示感谢,所以我将把它作为已回答的。然后尝试解决这个问题!谢谢Joshua。您可能会这样做的。”必须在类中导入UIKit。UIWindow是UIKit的一部分。