快速错误“无法将类型“接口控制器"的值分配为类型HKWorkoutSessionDelegate" [英] swift error "cannot assign value of type 'interface controller' to type HKWorkoutSessionDelegate'

查看:70
本文介绍了快速错误“无法将类型“接口控制器"的值分配为类型HKWorkoutSessionDelegate"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一条关于watchOS代码以测量HealthKit中的心率的错误消息,该错误消息在其他Xcode项目中运行正常.我已经比较了代码,它们看起来是一样的.该文件是InterfaceController.swift.

I have an error message arising for watchOS code to measure heart rate in HealthKit, that is working fine in a different Xcode project. I've compared the code and they seem the same. the file is InterfaceController.swift.

对于'self.workoutsession?.delegate = self'行,我收到红色标记错误无法将类型为'interface controller'的值分配为HKWorkoutSessionDelegate类型",有什么想法吗?

For the line 'self.workoutsession?.delegate = self' I get the red flag error "cannot assign value of type 'interface controller' to type HKWorkoutSessionDelegate" Any ideas?

这是该功能的代码.最后一个参数是带有错误的函数(先前的代码用于上下文).感谢您的帮助!

Here is the code for the function. The last para is the function with the error (The prior code is for context). Would appreciate your help!

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 没有实现 HKWorkoutSessionDelegate 吗?您没有显示代码的那部分.

self doesn't implement HKWorkoutSessionDelegate? You don't show that part of the code.

在实现必需的方法时,您必须进行更改

While you implemented the required methods, you have to change

类InterfaceController:WKInterfaceController {

成为

类InterfaceController:WKInterfaceController,HKWorkoutSessionDelegate {

告诉编译器该类实现了该协议.

which tells the compiler that the class implements that protocol.

这篇关于快速错误“无法将类型“接口控制器"的值分配为类型HKWorkoutSessionDelegate"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆