Home

Awesome

FHIR Swift Classes

These are Swift classes representing data models of 🔥 FHIR elements and resources, compatible with iOS 11 and OS X 10.13 and later.

This work is Apache licensed. FHIR® is the registered trademark of HL7 and is used with the permission of HL7.

Versioning

Due to the complications of combining two volatile technologies, here's an overview of which version numbers use which Swift and FHIR versions.

See tags/releases.

VersionSwiftFHIR 
4.25.0 Packages4.0.0-a53ec6ee1bR4
4.15.04.0.0-a53ec6ee1bR4
4.04.24.0.0-a53ec6ee1bR4
3.13.23.0.0.11832STU 3
3.03.03.0.0.11832STU 3
2.103.01.8.0.10521STU 3 Freeze, Jan 2017
2.93.01.6.0.9663STU 3 Ballot, Sep 2016
2.83.01.0.2.7202DSTU 2 (+ technical errata)
2.42.21.6.0.9663STU 3 Ballot, Sept 2016
2.32.31.0.2.7202DSTU 2 (+ technical errata)
2.2.32.21.0.2.7202DSTU 2 (+ technical errata)
2.22.0-2.21.0.2.7202DSTU 2 (+ technical errata)
2.12.0-2.21.0.1.7108DSTU 2
2.02.0-2.20.5.0.5149DSTU 2 Ballot, May 2015
1.01.20.5.0.5149DSTU 2 Ballot, May 2015
0.21.10.5.0.5149DSTU 2 Ballot, May 2015
0.11.00.0.81.2382DSTU 1

SMART on FHIR

The Swift-SMART framework utilizes these classes.

Progress

Here's a rough list of what still needs to be done.

[ ] Remove _isSummaryResource workaround to STU-3's nMin/isSummary errors
[ ] More convenience methods to working with resources in code
[ ] Nice support for simple PATCH operations
[ ] Separate resource models from base models; needs untangling of
    _owningResource, _owningBundle (easy), _resolved etc.
[ ] Handle resource versions nicely
[ ] Create a default behavior when a modifierExtension is detected
[ ] Update/modernize FHIRSearch
[ ] Search: report search parameters that the server ignored

Working, at least to some extent:

Naming Convention

Standard Swift naming conventions apply. Tabs are used for indentation and spaces for alignment – the best of both worlds. Classes representing FHIR resources do not have a prefix. Custom classes and protocols start with FHIR to not make them clash with element or resource classes and make them easily distinguishable.

FHIR Data Models

Classes are generated from FHIR resource definitions with our Python FHIR parser.

Verbousness

Swift is statically typed and introspection is very limited at the time. Therefore the generator needs to be a bit more verbose and create class-level serializers/deserializers, rather than looking at class properties at runtime and figure out how to serialize/deserialize.

Cardinality

Some data models have properties with a cardinality of a minimum of 1. While these can be enforced to never be nil in Swift by not making them Optionals, they are still optional to enable uniform initializers that only take a JSON dictionary. For classes representing models with non-optional properties, a convenience initializer is supplied to reflect the need to set those properties without enforcing it.

Contained Resources

FHIR makes use of contained resources. An extension on the Reference class is included that adds method to handle reference resolving.

To resolve resource references, call resolve(ModelClass) { resource in } on a reference property, which will return an instance of the referenced type in the callback if resolved successfully. To contain a resource and receive a Reference instance, call parent.containResource(contained)

// create a prescription with a contained medication
let order = MedicationRequest()
let medication = Medication()
medication.id = "med"
do {
    order.medicationReference = try order.containResource(medication)
}
catch let error {
    // failed to contain, either because no id or containing itself
}

// resolve the contained medication
order.medication?.resolve(Medication.self) { medication in
	if let medication = medication {
		// successfully resolved
	}
}

Search

The client supports the NoSQL-like approach proposed and used by fhir.js.

Compartments

Search can be restricted to compartments, these however are not yet supported in the SMART server nor in these classes.

[ ] Patient/23/procedure?date=>2010-01-01&date=<2011-12-31

Referenced (not yet implemented)

If search is restricted to a reference property, this applies:

Packaging

The full build of the framework will include all FHIR resources, which will result in a rather large binary. Take a look at the package.py script: provide one or more resource names when invoking the script from command line and it will output all the elements and resources that are needed for the desired resources. You may then be able to remove unnecessary resources, which unfortunately is a tedious task and requires fumbling with the factory.

There is an experimental SwiftFHIRMin-iOS build target which only includes a minimal set of resources. The problem here is that the factory is excluded and hence dereferencing, bundles and contained resources won't be properly instantiated.