-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathFruit.swift
More file actions
38 lines (32 loc) · 728 Bytes
/
Fruit.swift
File metadata and controls
38 lines (32 loc) · 728 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//
// Fruit.swift
// JSONCodable
//
// Created by Matthew Cheok on 13/10/15.
//
//
import JSONCodable
enum FruitColor: String {
case Red
case Blue
}
struct Fruit: Equatable {
let name: String
let color: FruitColor
}
func ==(lhs: Fruit, rhs: Fruit) -> Bool {
return lhs.name == rhs.name && lhs.color == rhs.color
}
extension Fruit: JSONCodable {
init(object: JSONObject) throws {
let decoder = JSONDecoder(object: object)
name = try decoder.decode("name")
color = try decoder.decode("color")
}
func toJSON() throws -> AnyObject {
return try JSONEncoder.create({ (encoder) -> Void in
try encoder.encode(name, key: "name")
try encoder.encode(color, key: "color")
})
}
}