smart-on-fhir / Swift-SMART

Swift SMART on FHIR framework for iOS and OS X
Other
134 stars 47 forks source link

Processing Bundled resources #10

Closed johncneal closed 7 years ago

johncneal commented 7 years ago

Using the code below, I've been trying to understand how to process a bundle of resources (which for test purposes I've loaded from a file). The bundle in question contains a Composition and a set of other resources which the Composition references internally. Stepping through the code in debug, at step //1 above I appear to have a valid Bundle resource (cofeolBundle). In it I can see its 'entry' array contains 62 SMART.BundlEntry(s). These represent the expected resources in the bundle, the first of which I know to be a Composition resource (through visual inspection of the json file).

At step //2, the XCode variables view shows 'anentry' is of type BundleEntry and further inspection of its 'resource' element shows it has the expected content of a Composition resource; attester, author, confidentiality, custodian, date, encounter etc etc. I can even see its 'section' array - which has 10 elements.

Clearly XCode is able to discern that anentry is a Composition resource but here's the problem - I dont seem to be able to refer to the anentry.resource.resourceType element in code!! This begs the question of how I step through and process this Composition and its associated bundled resources if I cant determine what type of resource is present in each BundleEntry?

I assume I'm missing some fundamental concept - either about Swift-FHIR or about how FHIR resource bundles should be processed in general. Any suggestions and observations would be much appreciated - its driving me nuts!

    let filename = "/Users/johnneal/somefile.json"
    let path: URL = URL(fileURLWithPath: filename)
    do {
        let data2 = try Data(contentsOf: path)
        do {
            let cofeolJSON = try JSONSerialization.jsonObject(with: data2, options: []) as? [String: Any]

            // 1
    let cofeolBundle = Bundle(json: cofeolJSON as FHIRJSON!)
    // 1

    // 2
            let anentry = cofeolBundle.entry![0]
            // 2          

        }
        catch{}
    }
    catch{}
p2 commented 7 years ago

What you're seeing is not Xcode's doing – it's the library that's reading the JSON and instantiating the correct classes. When you look at anentry.resource, you'll see that it already is a Composition class in your case, which tells you that the resourceType is a Composition. There is no need to look for resourceType. The class instances don't have a resourceType variable, but the class itself has one (because it's always the same, and it's always the same as the class name).

Also, FYI, you can just do:

let filename = "/Users/johnneal/somefile.json"
do {
    let bundle = try Bundle.instantiate(fromPath: filename)
    let anentry = cofeolBundle.entry![0]
}
catch {  }

Hope this helps.