2016-07-29 36 views
1

我有一個錯誤消息引起的watchOS代碼來測量HealthKit中的心率,這是在不同的Xcode項目中工作正常。代碼和它們看起來是一樣的,文件是InterfaceController.swiftswift error「無法分配類型'接口控制器'的值來鍵入HKWorkoutSessionDelegate'

對於'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!) 
    } 

回答

2

self不執行HKWorkoutSessionDelegate?你不顯示那部分代碼。

當你執行所需的方法,你必須改變

class InterfaceController: WKInterfaceController {

class InterfaceController: WKInterfaceController, HKWorkoutSessionDelegate {

告訴該類實現該協議的編譯器。

+0

好的,謝謝喬舒亞我會在上面添加代碼 –

+0

請看上面 –

+0

啊你說得對。在我的另一個Xcode項目中,我從中拷貝了它的確包含了我忽略的那一行。在這個Xcode項目中,它現在提出了另外9個紅色警報錯誤:在AppDelegate.swift上,它現在顯示「使用未聲明的類型'WCSessionDelegate',然後'使用未解析的標識符HKHealthStore'以及'使用未解析的標識符'。 (我將添加上面的代碼),並且在該swift文件中有6個「HKWorkoutSessionDelegate不可用」和「HKWorkoutSession不可用」的實例 –

相關問題