The RawRepresentable protocol
What is it and how to make use of it?
If you’ve ever used a Swift enum with a raw value, you may not even realise that you have already worked with the RawRepresentable
protocol. But what is it and how can you make use of it?
In this article we are going to investigate the RawRepresentable protocol and I’m going to show you some neat ways for using the protocol to our advantage.
Why are we talking about this?
The other day I’ve open sourced a really small package, SecureStore. Basically it’s just a keychain access wrapper for swift that is using the Security
framework from apple to read and write the keychain.
One of the convenience features of the package is that you can use any enum with a String
raw value as a key for saving or retrieving values from the store like this:
let value = store.retrieve(for: MyEnum.myKey)
The advantages of this may be pretty clear but let’s break them down real quick:
- The author of a package may not be able to provide an enum that describes all cases that the user needs. (Like my case with the secure store. You may have any string as a key)
- Using an enum prevents bugs like a typo in your string
- Of course, a string property on the type using the store can work just as well and there is really nothing wrong with that solution either, I just find it more convenient in many cases to have an enum with all the possible keys
So what about the RawRepresentable protocol?
As the documentation states, RawRepresentable
requires the conforming types to be able to convert to and from a raw value of the type T
. This means that a type conforming to it will have a property called rawValue
and an initialiser with a parameter rawValue
of type T
. Does that sound familiar?
Exactly, that’s just like an enum with a raw value and guess what.. an enum with a raw value does conform to the raw representable protocol.
Realising this, let’s see how to implement a function that takes any enum case as an argument:
func printRaw(_ value: any RawRepresentable<String>) {
print(“The value is \(value.rawValue)”)
}
And that’s it! Let’s test it
enum TestEnum: String {
case testCase = “testCase”
}
printRaw(TestEnum.testCase)
As you may have guessed already, the output of the call will be The value is testCase
.
But it is generic for a reason!
You are not limited to using strings here as the generic parameter of the protocol. It's just happens to be my most recent usecase. Any type of data your function needs, you can represent it as an enum case thus making a custom namespace for your parameters!
Conclusion
As you can see, this is not one of those world changing discoveries, but rather a small bit of spark to make your APIs a bit nicer to use. If you want to see this in action, or just need a simple way of working with keychain, feel free to check out SecureStore and if you have any questions about this or any other topics, reach out to me on X
Related articles
Here are some more articles that may interest you. Check them out!
Foundation Models
published on October 8, 2025
SwiftAILearn how to use Apple’s foundation models to run large language models locally on iOS. This article covers chat, streaming responses, structured data generation, and custom tools, helping you build secure and responsive AI features without relying on the cloud.
Read moreWorking with CoreBluetooth
published on September 30, 2025
SwiftiOSDiscover how to use CoreBluetooth in iOS to build apps that communicate with Bluetooth Low Energy devices. This article explains the roles of central and peripheral, how services and characteristics work, and provides a practical example for implementing both sides of BLE communication in Swift.
Read moreMatched Transitions in SwiftUI
published on September 23, 2025
SwiftSwiftUIAnimationsLearn how to use matchedGeometryEffect and navigationTransition in SwiftUI to create smooth, visually engaging transitions between views and screens. This article covers practical examples for synchronizing view geometry and implementing the new zoom navigation transition, helping you build more dynamic and polished UIs.
Read more