init(properties:id:uniquingKeysWith:)
Creates new generated content from the key-value pairs in the given sequence, using a combining closure to determine the value for any duplicate keys.
Declaration
init<S>(properties: S, id: GenerationID? = nil, uniquingKeysWith combine: (GeneratedContent, GeneratedContent) throws -> some ConvertibleToGeneratedContent) rethrows where S : Sequence, S.Element == (String, any ConvertibleToGeneratedContent)Parameters
- properties:
A sequence of key-value pairs to use for the new content.
- id:
A unique id associated with Generatedcontent.
- combine:
A closure that is called with the values to resolve any duplicates keys that are encountered. The closure returns the desired value for the final content.
Discussion
The order of properties is important. For Generable types, the order must match the order properties in the types schema.
You use this initializer to create generated content when you have a sequence of key-value tuples that might have duplicate keys. As the content is built, the initializer calls the combine closure with the current and new values for any duplicate keys. Pass a closure as combine that returns the value to use in the resulting content: The closure can choose between the two values, combine them to produce a new value, or even throw an error.
The following example shows how to choose the first and last values for any duplicate keys:
let content = GeneratedContent(
properties: [("name", "John"), ("name", "Jane"), ("married", true)],
uniquingKeysWith: { (first, _) in first }
)
// GeneratedContent(["name": "John", "married": true])