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
Copy file name to clipboardExpand all lines: README.md
+122Lines changed: 122 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -25,6 +25,7 @@ ObjectMapper is a framework written in Swift that makes it easy for you to conve
25
25
- Nested Objects (stand alone, in arrays or in dictionaries)
26
26
- Custom transformations during mapping
27
27
- Struct support
28
+
-[Immutable support](#immutablemappable-protocol-beta) (currently in beta)
28
29
29
30
# The Basics
30
31
To support mapping, a class or struct just needs to implement the ```Mappable``` protocol which includes the following functions:
@@ -144,6 +145,127 @@ ObjectMapper uses this function to get objects to use for mapping. Developers sh
144
145
145
146
If you need to implemented ObjectMapper in an extension, you will need to select this protocol instead of `Mappable`.
146
147
148
+
## `ImmutableMappable` Protocol (Beta)
149
+
150
+
> ⚠️ This feature is currently in Beta. There might be breaking API changes in the future.
151
+
152
+
`ImmutableMappable` provides an availability to map immutable properties. This is how `ImmutableMappable` differs from `Mappable`:
153
+
154
+
<table>
155
+
<tr>
156
+
<th>ImmutableMappable</th>
157
+
<th>Mappable</th>
158
+
</tr>
159
+
<tr>
160
+
<th colspan="2">Properties</th>
161
+
</tr>
162
+
<tr>
163
+
<td>
164
+
<pre>
165
+
<strong>let</strong> id: Int
166
+
<strong>let</strong> name: String?
167
+
</pre>
168
+
</td>
169
+
<td>
170
+
<pre>
171
+
var id: Int!
172
+
var name: String?
173
+
</pre>
174
+
</td>
175
+
</tr>
176
+
<tr>
177
+
<th colspan="2">JSON -> Model</th>
178
+
</tr>
179
+
<tr>
180
+
<td>
181
+
<pre>
182
+
init(map: Map) <strong>throws</strong> {
183
+
id = <strong>try</strong> map.value("id")
184
+
name = <strong>try?</strong> map.value("name")
185
+
}
186
+
</pre>
187
+
</td>
188
+
<td>
189
+
<pre>
190
+
mutating func mapping(map: Map) {
191
+
id <- map["id"]
192
+
name <- map["name"]
193
+
}
194
+
</pre>
195
+
</td>
196
+
</tr>
197
+
<tr>
198
+
<th colspan="2">Model -> JSON</th>
199
+
</tr>
200
+
<tr>
201
+
<td>
202
+
<pre>
203
+
mutating func mapping(map: Map) {
204
+
id <strong>>>></strong> map["id"]
205
+
name <strong>>>></strong> map["name"]
206
+
}
207
+
</pre>
208
+
</td>
209
+
<td>
210
+
<pre>
211
+
mutating func mapping(map: Map) {
212
+
id <- map["id"]
213
+
name <- map["name"]
214
+
}
215
+
</pre>
216
+
</td>
217
+
</tr>
218
+
<tr>
219
+
<th colspan="2">Initializing</th>
220
+
</tr>
221
+
<tr>
222
+
<td>
223
+
<pre>
224
+
<strong>try</strong> User(JSONString: JSONString)
225
+
</pre>
226
+
</td>
227
+
<td>
228
+
<pre>
229
+
User(JSONString: JSONString)
230
+
</pre>
231
+
</td>
232
+
</tr>
233
+
</table>
234
+
235
+
#### `init(map: Map) throws`
236
+
237
+
This throwable initializer is used to map immutable properties from the given `Map`. Every immutable properties should be initialized in this initializer.
238
+
239
+
This initializer throws an error when ...
240
+
241
+
- ... failed to pop a value from the `Map`
242
+
- ... failed to transform a value using `Transform`
243
+
244
+
`ImmutableMappable` uses `Map.value(_:using:)` method to get values from the `Map`. This method should be used with `try` keyword because it is throwable. `Optional` properties could be easily handled using `try?`.
245
+
246
+
```swift
247
+
init(map: Map) throws {
248
+
name =try map.value("name") // throws an error when fails
249
+
createdAt =try map.value("createdAt", using: DateTransform()) // throws an error when fails
This method is where the reverse transform is performed. Since immutable properties are not mapped with `<-` operator, developers have to map reverse transform manually using `>>>` operator.
258
+
259
+
```swift
260
+
mutatingfuncmapping(map: Map) {
261
+
name >>> map["name"]
262
+
createdAt >>> (map["createdAt"], DateTransform())
263
+
updatedAt >>> (map["updatedAt"], DateTransform())
264
+
posts >>> map["posts"]
265
+
}
266
+
```
267
+
268
+
147
269
# Easy Mapping of Nested Objects
148
270
ObjectMapper supports dot notation within keys for easy mapping of nested objects. Given the following JSON String:
0 commit comments