快速错误“无法将类型“接口控制器”的值分配为类型HKWorkoutSessionDelegate”

迪米特里

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

对于“ self.workoutsession?.delegate = self”行,我收到了红旗错误“无法将类型为'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!)
    }
约书亚·史密斯(Joshua Smith)

self没有实现HKWorkoutSessionDelegate您没有显示代码的那部分。

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

class InterfaceController: WKInterfaceController {

成为

class InterfaceController: WKInterfaceController, HKWorkoutSessionDelegate {

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

快速操场中的错误:无法将类型“Int”的值分配给类型“[Int]”

AngularJS和Typescript:如何将类/类型分配为指令的控制器?

无法将“ MenuView”类型的值分配为“ some View”类型

无法将类型“ IndexPath”的值分配为类型“ Int”

尝试锁定到导航视图控制器时发生错误,无法强制将“ UIViewController”类型的值转换为“ UINavigationController”类型

尝试在 2 个控制器之间传递数据时,出现错误无法分配类型“ViewController.Item?”的值 输入“项目?”,

无法分配类型为“ AnyObject?”的值 到“ String!”类型的值 -快速解析

无法将类型为“字符串”的值分配为类型为“ UILabel?”

无法将类型为'ArraySlice <User>'的值分配为类型为[[User]'

引用类型“ _”的错误:“无法将类型“ [Value]”的值分配给类型“ _?””

错误:无法分配类型为“ AnyObject?”的值 到“ NSURL”类型的值

Swift:无法为“ int”类型的值分配“ int”类型的值?错误

IntelliSense:无法将类型为“ void”的值分配给类型为“ double”的实体

无法将类型为int的值分配给类型为node的实体

Typescript 节点快速控制器。什么类型的回报必须是?

在快速机器学习中分配数组无法将类型[[[String]]'的值分配给类型'String?'

快速错误:无法将“字符”类型的值转换为预期的参数类型“ Unicode.Scalar”

无法分配类型为“ NSData!”的值!到“ AnyObject”类型的值?

无法分配类型为'MDLMaterialProperty ?!'的值 到'Int'类型的值

无法将NetworkImage分配为Widget类型

我遇到错误无法分配类型的值

无法将类型[[[String:AnyObject]]]的值分配为类型[[String:AnyObject?]]]

无法将“某些视图”类型的值分配为“某些视图”类型

如何解决“状态错误(活动)E0513无法将类型为“ const wchar_t *”的值分配给类型为“ wchar_t *”的实体”

XamlCompiler错误WMC0055:无法将文本值“ 10.0”分配给类型为“十进制”的属性“ xx”

XamlCompiler错误WMC0055:无法将文本值“帮助”分配给类型为“ IconElement”的属性“ Icon”

错误:无法将“List<dynamic>”类型的值分配给“List<int>”类型的变量

获取“无法将类型 'CLLocationDegrees'(又名'Double')的值分配给类型'Int'”错误(Swift 4)

错误:无法将类型“ void *”的值分配给类型“ int **”的实体