快速错误“无法将类型“接口控制器”的值分配为类型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条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

Swift无法将类型'()'的值分配为类型'String?'

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

图像选择器委托错误:无法分配类型的值

来自分类Dev

无法将类型为(()-> Void)的值分配为类型为((()-> Void)!''

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

无法将类型为“ const char *”的值分配给类型为“ char”的实体C OOP

来自分类Dev

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

来自分类Dev

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

来自分类Dev

快速使用.filter():错误:无法将类型'([[V])-> Bool'的值转换为预期的参数类型'([__))-> Bool'

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

Swift错误:无法将UIColor类型的值分配给CGColor类型

来自分类Dev

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

来自分类Dev

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

来自分类Dev

错误:无法将“AuthResult”类型的值分配给“FirebaseUser”类型的变量

Related 相关文章

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

    Swift无法将类型'()'的值分配为类型'String?'

  5. 5

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

  6. 6

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

  7. 7

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

  8. 8

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

  9. 9

    图像选择器委托错误:无法分配类型的值

  10. 10

    无法将类型为(()-> Void)的值分配为类型为((()-> Void)!''

  11. 11

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

  12. 12

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

  13. 13

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

  14. 14

    无法将类型为“ const char *”的值分配给类型为“ char”的实体C OOP

  15. 15

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

  16. 16

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

  17. 17

    快速使用.filter():错误:无法将类型'([[V])-> Bool'的值转换为预期的参数类型'([__))-> Bool'

  18. 18

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

  19. 19

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

  20. 20

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

  21. 21

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

  22. 22

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

  23. 23

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

  24. 24

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

  25. 25

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

  26. 26

    Swift错误:无法将UIColor类型的值分配给CGColor类型

  27. 27

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

  28. 28

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

  29. 29

    错误:无法将“AuthResult”类型的值分配给“FirebaseUser”类型的变量

热门标签

归档