2017-08-11 17 views
0

私は、睡眠分析に関する以下のスウィフトスクリプトを持っています。スイフト:未解決の識別子の使用。対象メンバーシップ

エラー:

View Controller has no member 'updateTime'.

私は、ファイルインスペクタを使用して、このターゲットを追加し、会員を対象とし、それを追加しようとしたが、ターゲット自体が奇妙を超えており、表示されません。どんなフィードバックも高く評価されます。

PS:「エラー」はif != nilステートメントの未解決の識別子ではないという別のエラーメッセージがポップアップし続けます。ここのお手伝いも高く評価されます。

import UIKit 
import HealthKit 

    let healthStore = HKHealthStore() 
class ViewController: UIViewController { 
    @IBOutlet var displayTimeLabel: UILabel! 

    var startTime = TimeInterval() 
    var timer:Timer = Timer() 
    var endTime: NSDate! 
    var alarmTime: NSDate! 

    func saveSleepAnalysis() { 

     //1. startTime(alarmTime) and endTime are NSDate Objects// 
     if let sleepType = HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.sleepAnalysis) { 
      //we create a new object that we want to add into our Health app(This is our INBED object)// 
      let object1 = HKCategorySample(type:sleepType, value: HKCategoryValueSleepAnalysis.inBed.rawValue, start: self.alarmTime as Date, end: self.endTime as Date) 
      // Time to save the object// 
      healthStore.save(object1, withCompletion: { (success, errpr) -> Void in 

      if error != nil 
       { 
        return 
       } 

       if success { 
        print("My new data was saved in HealthKit") 

       } else { 
        //something happened again// 
       } 
      }) 
      //This our ASLEEP object// 
      let object2 = HKCategorySample(type:sleepType, value: HKCategoryValueSleepAnalysis.asleep.rawValue, start: self.alarmTime as Date, end: self.endTime as Date) 
      //now we save our objects to our mainLibrary known as HealthStore 
      healthStore.save(object2, withCompletion: { (success, error) -> Void in 
       if error != nil { 
         //Something went wrong// 
        return 

        if success { 
         print("My new data (2: Asleep data) was saved into HealthKit") 
        } else { 
          //something happened again// 
        } 
       } 
     } 
    )} 

     func retrieveSleepAnalysis() { 
      //first, define our object type that we watn again in BOOLEAN FORMAT// 
      if let sleepType = HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.sleepAnalysis) { 

       //use sortDescriptor to get teh recent data first: so from MostRecentData to PastData// 
       let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false) 

       //we create our query with a block completion to execute 
       let query = HKSampleQuery(sampleType: sleepType, predicate: nil, limit:30, sortDescriptors: [sortDescriptor]) { (query, tmpResult, error) -> Void in 

        if error != nil { 
         //something happends// 
         return 
        } 
        if let result = tmpResult { 

         //then i want the computer to do something with my data// 
         for item in result { 
          if let sample = item as? HKCategorySample { 
           let value = (sample.value == HKCategoryValueSleepAnalysis.inBed.rawValue) ? "InBed" : "Asleep" 
           print("Healthkit sleep: \(sample.startDate) \(sample.endDate) = value: \(value)") 
          } 
         } 
        } 
       } 

       //finally, we execute our query: Print out our output file // 
       healthStore.execute(query) 
      } 
     } 
     func viewDidLoad() { 
     super.viewDidLoad() 

     let typestoRead = Set([ 
      HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.sleepAnalysis)! 
      ]) 

     let typestoShare = Set([ 
      HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.sleepAnalysis)! 
      ]) 
      healthStore.requestAuthorization(toShare: typestoShare, read: typestoRead) { (success, error) -> Void in 
      if success == false { 
        NSLog(" Display not allowed") 
      } 
     } 
    } 


func updateTime() { 
     let currentTime = NSDate.timeIntervalSinceReferenceDate 

     //Find the difference between current time and start time. 
     var elapsedTime: TimeInterval = currentTime - startTime 

     //calculate the minutes in elapsed time. 
     let minutes = UInt8(elapsedTime/60.0) 
     elapsedTime -= (TimeInterval(minutes) * 60) 

     //calculate the seconds in elapsed time. 
     let seconds = UInt8(elapsedTime) 
     elapsedTime -= TimeInterval(seconds) 

     //find out the fraction of milliseconds to be displayed. 
     let fraction = UInt8(elapsedTime * 100) 

     //add the leading zero for minutes, seconds and millseconds and store them as string constants 

     let strMinutes = String(format: "%02d", minutes) 
     let strSeconds = String(format: "%02d", seconds) 
     let strFraction = String(format: "%02d", fraction) 

     //concatenate minuets, seconds and milliseconds as assign it to the UILabel 
     displayTimeLabel.text = "\(strMinutes):\(strSeconds):\(strFraction)" 
    } 


     func start(sender: AnyObject) { 
      alarmTime = NSDate() 
      if (!timer.isValid) { 
       let Selector : Selector = #selector(ViewController.updateTime) 
       timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: Selector, userInfo: nil, repeats: true) 
       startTime = NSDate.timeIntervalSinceReferenceDate 
      } 

     } 



     func stop(sender: AnyObject) { 
      endTime = NSDate() 
      saveSleepAnalysis() 
      retrieveSleepAnalysis() 
      timer.invalidate() 
     } 

     func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

     } 
    } 
+0

であなたのコードを置き換えあなたには、いくつかのインデントと可能な範囲の問題がある可能性がありますように見えます。関数updateTimeがクラスで定義されていることは確かですか? – ryantxr

答えて

0

この

import UIKit 
import HealthKit 

let healthStore = HKHealthStore() 
class ViewController: UIViewController { 
    @IBOutlet var displayTimeLabel: UILabel! 

    var startTime = TimeInterval() 
    var timer:Timer = Timer() 
    var endTime: NSDate! 
    var alarmTime: NSDate! 

    func saveSleepAnalysis() { 

     //1. startTime(alarmTime) and endTime are NSDate Objects// 
     if let sleepType = HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.sleepAnalysis) { 
      //we create a new object that we want to add into our Health app(This is our INBED object)// 
      let object1 = HKCategorySample(type:sleepType, value: HKCategoryValueSleepAnalysis.inBed.rawValue, start: self.alarmTime as Date, end: self.endTime as Date) 
      // Time to save the object// 
      healthStore.save(object1, withCompletion: { (success, errpr) -> Void in 

       if errpr != nil 
       { 
        return 
       } 

       if success { 
        print("My new data was saved in HealthKit") 

       } else { 
        //something happened again// 
       } 
      }) 
      //This our ASLEEP object// 
      let object2 = HKCategorySample(type:sleepType, value: HKCategoryValueSleepAnalysis.asleep.rawValue, start: self.alarmTime as Date, end: self.endTime as Date) 
      //now we save our objects to our mainLibrary known as HealthStore 
      healthStore.save(object2, withCompletion: { (success, error) -> Void in 
       if error != nil { 
        //Something went wrong// 
        return 

        if success { 
         print("My new data (2: Asleep data) was saved into HealthKit") 
        } else { 
         //something happened again// 
        } 
       } 
      } 
      )} 


    } 

    func retrieveSleepAnalysis() { 
     //first, define our object type that we watn again in BOOLEAN FORMAT// 
     if let sleepType = HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.sleepAnalysis) { 

      //use sortDescriptor to get teh recent data first: so from MostRecentData to PastData// 
      let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false) 

      //we create our query with a block completion to execute 
      let query = HKSampleQuery(sampleType: sleepType, predicate: nil, limit:30, sortDescriptors: [sortDescriptor]) { (query, tmpResult, error) -> Void in 

       if error != nil { 
        //something happends// 
        return 
       } 
       if let result = tmpResult { 

        //then i want the computer to do something with my data// 
        for item in result { 
         if let sample = item as? HKCategorySample { 
          let value = (sample.value == HKCategoryValueSleepAnalysis.inBed.rawValue) ? "InBed" : "Asleep" 
          print("Healthkit sleep: \(sample.startDate) \(sample.endDate) = value: \(value)") 
         } 
        } 
       } 
      } 

      //finally, we execute our query: Print out our output file // 
      healthStore.execute(query) 
     } 
    } 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     let typestoRead = Set([ 
      HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.sleepAnalysis)! 
      ]) 

     let typestoShare = Set([ 
      HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.sleepAnalysis)! 
      ]) 
     healthStore.requestAuthorization(toShare: typestoShare, read: typestoRead) { (success, error) -> Void in 
      if success == false { 
       NSLog(" Display not allowed") 
      } 
     } 
    } 


    func updateTime() { 
     let currentTime = NSDate.timeIntervalSinceReferenceDate 

     //Find the difference between current time and start time. 
     var elapsedTime: TimeInterval = currentTime - startTime 

     //calculate the minutes in elapsed time. 
     let minutes = UInt8(elapsedTime/60.0) 
     elapsedTime -= (TimeInterval(minutes) * 60) 

     //calculate the seconds in elapsed time. 
     let seconds = UInt8(elapsedTime) 
     elapsedTime -= TimeInterval(seconds) 

     //find out the fraction of milliseconds to be displayed. 
     let fraction = UInt8(elapsedTime * 100) 

     //add the leading zero for minutes, seconds and millseconds and store them as string constants 

     let strMinutes = String(format: "%02d", minutes) 
     let strSeconds = String(format: "%02d", seconds) 
     let strFraction = String(format: "%02d", fraction) 

     //concatenate minuets, seconds and milliseconds as assign it to the UILabel 
     displayTimeLabel.text = "\(strMinutes):\(strSeconds):\(strFraction)" 
    } 


    func start(sender: AnyObject) { 
     alarmTime = NSDate() 
     if (!timer.isValid) { 
      let Selector : Selector = #selector(ViewController.updateTime) 
      timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: Selector, userInfo: nil, repeats: true) 
      startTime = NSDate.timeIntervalSinceReferenceDate 
     } 

    } 



    func stop(sender: AnyObject) { 
     endTime = NSDate() 
     saveSleepAnalysis() 
     retrieveSleepAnalysis() 
     timer.invalidate() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

} 
+0

このコードの問題は50行目です:「戻り値の後のコードは決して実行されません」 – Flavia

+0

私のコードの50行ですか?あなたの問題はUpdateTimeを参照することができませんでした。私のコードでは別の問題がある場合はその問題を解決しました。私はそのコードをビルドしていませんでしたので、私はちょうどUpdateTime問題 –

+0

を解決しました。それはソフトウェア自体の問題であることが判明しました、あなたの助けに感謝します。 – Flavia

関連する問題