Search code examples
iosjsonresearchkit

How to use ORKESerializer in my app?


I am working on developing my first ResearchKit App. I have been watching this video. One of the techniques used that is going to be helpful for me is serializing the results of a survey to JSON. The method used in the video is ORKESerializer.JSONDataForObject(taskResult). He explains that this is not a standard part of researchKit, but it was included in a test app, called ORKTest that is on GitHub.

I set up my taskViewController delegate just like he had it set on the video, like this:

extension ViewController : ORKTaskViewControllerDelegate {

    func taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?) {
        switch reason {
        case .Completed:
            let taskResult = taskViewController.result

            let jsonData = try! ORKESerializer.JSONDataForObject(taskResult)
            if let jsonString = NSString(data: jsonData, encoding: NSUTF8StringEncoding) {
                print(jsonString)
            }
            break

        case .Failed, .Discarded, .Saved:
            break

        }
        //Handle results with taskViewController.result
//        let taskResult = taskViewController.result
        taskViewController.dismissViewControllerAnimated(true, completion: nil)
    }

}

I am getting this error upon compiling : use of unresolved identifier: ORKESerializer

So in the ORKTest app, in the GitHub files, I found 2 files. One called ORKESerialization.h, and one called ORKESerialization.m. I tried dragging those into my project, as I saw those files in the man's project in the video. And then that also prompted me to create a bridging header file as well, which I also saw in his project.

shows files included in my project

After doing that I am still getting the same error. The truth is I don't know exactly how to include these serialization packages with my app. Does anyone know how to included the right files so that I can implement this ORKEserialization method?

Thanks!


Solution

  • You need to import ORKESerialization.h in your bridging header:

      #import "ORKESerialization.h"