2014-12-27 3 views
0

완성 처리기의 결과를 그래프 라이브러리에 플로팅하는 데 사용할 수 있도록 지정하려고합니다. 현재 데이터는 콘솔 출력으로 콘솔에 인쇄 :완성 처리기의 결과를 변수에 어떻게 할당 할 수 있습니까?

[(200.0, 2014년 12월 26일 12시 44분 0초 0000), 300.0, 2014년 12월 25일 12시 44분 0초 0000)]

- 질문 : 플롯에서이 데이터를 사용해야합니다. .

  • VAR dateArray = query.performHKQuery() 날짜
  • VAR 스텝 어레이 = query.performHKQuery (:이에 변수 비슷한에 데이터를 할당 할 수 있도록 나는의 ViewController 또는 QueryHKArray2 내 코드를 변경하는 방법) .stepCount

도움이 되었습니까?

의 ViewController :

import UIKit 


class ViewControllerArray2: UIViewController { 

    var query = QueryHKArray2() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     printStepsAndDate() 
    } 


    func printStepsAndDate() { 
     query.performHKQuery() { stepCount in 
      println(stepCount) 

     } 
    } 

QueryHKArray2 클래스 :

import UIKit 
import HealthKit 

class QueryHKArray2: NSObject { 

    func performHKQuery(completion: ([(steps: Double, date: NSDate)]) -> Void) { 

     let healthKitManager = HealthKitManager.sharedInstance 
     let stepsSample = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount) 
     let stepsUnit = HKUnit.countUnit() 
     let sampleQuery = HKSampleQuery(
      sampleType: stepsSample, 
      predicate: nil, 
      limit: 0, 
      sortDescriptors: nil) 
      { 
       (sampleQuery, samples, error) in 

       if let samples = samples as? [HKQuantitySample] { 

        let steps = samples.map { (sample: HKQuantitySample)->(steps: Double, date: NSDate) in 
         let stepCount = sample.quantity.doubleValueForUnit(stepsUnit) 
         let date = sample.startDate 
         return (steps: stepCount, date: date) 
        } 

        completion(steps) 
       } 

     } 
     healthKitManager.healthStore.executeQuery(sampleQuery) 
    } } 

답변

1

할 수 있습니다 루프 stepCount을 통해하고 tuple 값을

func printStepsAndDate() { 
    var dateArray:[NSDate] = [] 
    var stepArray:[Double]= [] 
    query.performHKQuery() { stepCount in 
     for temp in stepCount { 
      stepArray.append(temp.steps) 
      dateArray.append(temp.date) 
     } 

    } 
} 
+0

감사를 얻을 수 이제 dateArray 및 stepArray는 함수 내에서만 범위 내에 있습니다. 내가 그들을 밖으로 시작한 다음 나중에 호출해도 나는 아무것도 얻지 못한다. dateArray 및 stepArray 데이터를 함수 범위 외부에서 액세스 할 수 있습니까? – KML

+0

@karlml은 인스턴스 변수를 만들고 쿼리를 가져온 후에 만 ​​액세스합니다. – codester

+0

코드 작성자 주셔서 감사합니다. – KML

관련 문제