Join our Newsletter — 33% off our NHI Course
Home› Glossary› Foundations & NHI Taxonomy› Raw Value Initializer
Foundations & NHI Taxonomy

Raw Value Initializer

← Back to Glossary
By NHI Mgmt Group Updated November 3, 2026 Domain: Foundations & NHI Taxonomy

A raw value initializer creates an enumeration case from its underlying raw value, such as a string or integer. In Swift, it returns an optional because the value only maps successfully when it matches a defined case exactly.

How raw value initializers work

A raw value initializer is the bridge between an enumeration’s stored raw representation and its typed cases. It does not invent a new case, it asks whether the supplied raw value exactly matches a declared case and, if so, returns that case as a value the program can work with.

That exact-match behavior is what makes the initializer useful for deserialising external input, configuration values, and simple lookup logic. If the raw value does not correspond to a case, the initializer fails cleanly rather than forcing an invalid enum value into the program.

Why the initializer is optional

The optional return type is the key design choice. An enum with raw values has a closed set of valid cases, but incoming data may be incomplete, malformed, or simply outside that set. Returning an optional lets the caller handle absence explicitly instead of assuming every value is valid.

In practice, that means the caller must decide whether to provide a fallback, reject the input, or surface an error. The initializer therefore sits at the boundary between trusted code and uncertain data, where strict matching is safer than implicit coercion.

Exact matching and type safety

Raw value initializers preserve the main advantage of enums: type safety. A string or integer on its own carries no semantic meaning, but once it maps to an enum case, the rest of the code can switch on a known set of possibilities without scattering magic values throughout the program.

This exactness also keeps behavior predictable. Two raw values that look similar but are not identical, such as different spellings or capitalisation, will not resolve to the same case. That is deliberate, because the mapping should reflect the enum definition rather than a guess about intent.

Common uses and edge cases

Developers often use raw value initializers when parsing payloads, reading stored preferences, or converting identifiers from APIs into domain types. The initializer is a compact way to move from a stringly typed world into a constrained model where only defined cases can exist.

Edge cases usually appear when the raw data source evolves faster than the code. A new external value, a renamed case, or an unexpected format change can cause the initializer to return nil. That failure is useful because it exposes a mismatch early, before bad data spreads deeper into the application.

Practitioner Guidance

Why practitioners should care: Raw value initializers are often the first validation step when data crosses a boundary into application logic. Treat nil as a meaningful signal, not just a convenience, because it tells you the input did not match the enum’s approved values.

Common misunderstanding: A raw value initializer does not perform fuzzy matching, normalisation, or implicit conversion. If your source data needs translation, do that explicitly before or after the initializer so the mapping stays intentional and reviewable.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

NHIMG Editorial Note
Reviewed and updated by the NHIMG editorial team on November 3, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org