我收到了一条错误消息,watchos代码在healthkit中测量心率,在另一个xcode项目中运行良好。我比较了代码,它们看起来是一样的。文件是interfacecontroller.swift。
在“self.workoutsession”这行吗?.delegate=self“i get the red flag error”无法将“interface controller”类型的值分配给hkworkoutsessiondelegate“有什么想法吗?”
这是函数的代码。最后一段是出现错误的函数(前面的代码用于上下文)。谢谢你的帮助!

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?你没有显示代码的那部分。
在实现所需的方法时,必须更改
class InterfaceController: WKInterfaceController {
成为
class InterfaceController: WKInterfaceController, HKWorkoutSessionDelegate {
它告诉编译器类实现了该协议。

关于swift - swift 错误“无法将类型'接口(interface) Controller '的值分配为类型HKWorkoutSessionDelegate',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38662225/

10-10 14:07
查看更多