### Use ResourceProxy to instantiate a FHIR model Source: https://github.com/apple/fhirmodels/blob/main/HowTo/Instantiation.md This example demonstrates how to use ResourceProxy to dynamically instantiate FHIR models from JSON data. It shows how to get a generic Resource, a typed resource if available, and how to use pattern matching with the proxy. ```swift import ModelsR4 let data = let decoder = JSONDecoder() do { let proxy = try decoder.decode(ResourceProxy.self, from: data) let resource = proxy.get() // `resource` is a generic `Resource` let patient = proxy.get(if: Patient.self) // `patient` is a nullable `Patient` if case .patient(let patient) = proxy { // `patient` is a `Patient` } switch proxy { case .patient(let patient): // `patient` is a `Patient` break default: break } } catch { print("Failed to instantiate: \(error)") } ``` -------------------------------- ### Get resources from a FHIR Bundle Source: https://github.com/apple/fhirmodels/blob/main/HowTo/Instantiation.md This example demonstrates how to parse a FHIR Bundle and extract specific resource types, such as Observations, from its entries. ```swift import ModelsR4 let data = let bundle = try JSONDecoder().decode(ModelsR4.Bundle.self, from: data) let observations = bundle.entry?.compactMap { $0.resource?.get(if: ModelsR4.Observation.self) } // observations is an array of `Observation` instances ``` -------------------------------- ### Working with Value-X Source: https://github.com/apple/fhirmodels/blob/main/HowTo/ModelProperties.md Example of decoding a MedicationStatement and checking the type of the medication property (reference or codeableConcept). ```swift let med = try decoder.decode(MedicationStatement.self, from: data) if case .reference(let ref) = med.medication { print("--> medication reference display: \(ref.display ?? "{nil}")") } else if case .codeableConcept(let cc) = med.medication { print("--> medication codeable concept text: \(cc.text ?? "{nil}")") // Note: you should also look at `cc.coding` } ``` -------------------------------- ### Example usage of the mothersFamilyName extension Source: https://github.com/apple/fhirmodels/blob/main/HowTo/Extensions.md Demonstrates how to decode a FHIR Patient resource from a JSON string and use the `mothersFamilyName` extension to print the patient's family name and mother's family name. ```swift import ModelsR4 let string = """ { \"resourceType\": \"Patient\", \"id\": \"2429563849\", \"name\": [ { \"use\": \"official\", \"family\": \"SMITH\", \"_family\": { \"extension\": [ { \"url\": \"http://hl7.org/fhir/StructureDefinition/humanname-mothers-family\", \"valueString\": \"JOHNSON\" } ] }, \"given\": [ \"JOHN\" ] } ] } """ guard let data = string.data(using: .utf8) else { throw ... } let decoder = JSONDecoder() let patient = try decoder.decode(Patient.self, from: data) for name in patient.name ?? [] { print("family: \(name.family?.value?.string ?? \"{nil}\"), mother's family: \(name.family?.mothersFamilyName ?? \"{nil}")") } // prints: "family: SMITH, mother's family: JOHNSON" ``` -------------------------------- ### Working with Primitives - Getting String Value Source: https://github.com/apple/fhirmodels/blob/main/HowTo/ModelProperties.md Shows how to extract the native Swift String value from a FHIRPrimitive. ```swift let id = resource.id?.value?.string ``` -------------------------------- ### Decode to a known FHIR Resource type Source: https://github.com/apple/fhirmodels/blob/main/HowTo/Instantiation.md This example shows how to directly decode JSON data into a specific FHIR resource type, like Patient, when the resource type is already known. ```swift import ModelsR4 let data = let decoder = JSONDecoder() do { let resource = try decoder.decode(Patient.self, from: data) } catch { print("Failed to instantiate Patient: \(error)") } ``` -------------------------------- ### Working with Primitives - Extension Method Source: https://github.com/apple/fhirmodels/blob/main/HowTo/ModelProperties.md Shows how to use the asFHIR{type}Primitive() extension method on Swift native types like URL and String. ```swift let url = URL(string: "http://apple.com")!.asFHIRURIPrimitive() // url is a `FHIRPrimitive` let str = "http://hl7.org/fhir".asFHIRURIPrimitive() // str is a `FHIRPrimitive?` ``` -------------------------------- ### Date Time Conversion Source: https://github.com/apple/fhirmodels/blob/main/HowTo/DateAndTime.md Demonstrates parsing a date-time string into a DateTime object and accessing its components. ```swift do { let dateTimeString = "2020-03-12T12:33:54.6543-06:00" let dateTime = try DateTime(dateTimeString) dateTime.date.year == 2020 dateTime.time.minute == 33 dateTime.timeZone == TimeZone(secondsFromGMT: -6 * 3600) } catch { print("Failed to parse date time string: \(error)") } ``` -------------------------------- ### Working with Primitives - Direct Comparison Source: https://github.com/apple/fhirmodels/blob/main/HowTo/ModelProperties.md Illustrates comparing a FHIRPrimitive directly with a String literal. ```swift if resource.id == "101" { } ``` -------------------------------- ### Package.swift dependency Source: https://github.com/apple/fhirmodels/blob/main/README.md How to add FHIRModels to your Package.swift file as a dependency. ```swift dependencies: [ .package(url: "https://github.com/apple/FHIRModels.git", .upToNextMajor(from: "0.9.1")) ] ``` -------------------------------- ### Working with Primitives - Assignment Source: https://github.com/apple/fhirmodels/blob/main/HowTo/ModelProperties.md Demonstrates assigning String and Bool literals to FHIR primitive properties. ```swift let patient = Patient(...) patient.id = "101" patient.active = true ``` -------------------------------- ### Patient with a Name Source: https://github.com/apple/fhirmodels/blob/main/HowTo/ModelProperties.md Demonstrates instantiating a HumanName and Patient using ExpressibleByLiteral for primitive types. ```swift import ModelsR4 let name = HumanName(family: "Altick", given: ["Kelly"]) let patient = Patient(name: [name]) name.given?.first // FHIRPrimitive? name.given?.first?.value // FHIRString? name.given?.first?.value?.string // String? ``` -------------------------------- ### Swift Extension for FHIRPrimitive to access mother's family name Source: https://github.com/apple/fhirmodels/blob/main/HowTo/Extensions.md This Swift extension adds a computed property `mothersFamilyName` to `FHIRPrimitive` to extract the mother's family name from a specific FHIR extension. ```swift extension FHIRPrimitive { var mothersFamilyName: String? { guard let mothersFamilyExtension = extensions(for: "http://hl7.org/fhir/StructureDefinition/humanname-mothers-family").first else { return nil } if case .string(let str) = mothersFamilyExtension.value { return str.value?.string } // Not a `valueString` return nil } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.