You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
All `Storage` now are generic by default, so you can get a type safety experience. Once you create a Storage, it has a type constraint that you don't need to specify type for each operations afterwards.
80
+
`
81
+
If you want to change the type, `Cache` offers `transform` functions, look for `Transformer` and TransformerFactory` for built-in transformers.
82
+
83
+
```swift
84
+
let storage: Storage<User> =...
85
+
storage.setObject(superman, forKey: "user")
86
+
87
+
let imageStorage = storage.transformImage() // Storage<UIImage>
88
+
imageStorage.setObject(image, forKey: "image")
89
+
90
+
let stringStorage = storage.transformCodable(ofType: String.self) // Storage<String>
Each transformation allows you to work with a specific type, however the underlying caching mechanism remains the same, you're working with the same `Storage`, just with different type annotation. You can also create different `Storage` for each type if you want.
95
+
96
+
`Transformer` is necessary because the need of serialising and deserialising objects to and from `Data` for disk persistency. `Cache` provides default `Transformer ` for `Data`, `Codable` and `UIImage/NSImage`
97
+
73
98
#### Codable types
74
99
75
100
`Storage` supports any objects that conform to [Codable](https://developer.apple.com/documentation/swift/codable) protocol. You can [make your own things conform to Codable](https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types) so that can be saved and loaded from `Storage`.
@@ -102,6 +127,8 @@ public enum StorageError: Error {
102
127
caseencodingFailed
103
128
/// The storage has been deallocated
104
129
casedeallocated
130
+
/// Fail to perform transformation to or from Data
0 commit comments