Created
June 8, 2017 06:04
-
-
Save hanawat/d3064eade45285d41d09f64ef7ee9bcd to your computer and use it in GitHub Desktop.
Swift4.0 で追加される Codable ref: http://qiita.com/hanawat/items/f3fffac2a4ea2e8a1767
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Person { | |
let name: String | |
let age: Int | |
} | |
extension Person { | |
class Helper: NSObject, NSCoding { | |
let person: Person? | |
init(person: Person) { | |
self.person = person | |
super.init() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
guard | |
let name = aDecoder.decodeObject(forKey: "name") as? String, | |
let age = aDecoder.decodeObject(forKey: "age") as? Int else { | |
return nil | |
} | |
person = Person(name: name, age: age) | |
super.init() | |
} | |
func encode(with aCoder: NSCoder) { | |
guard let person = person else { return } | |
aCoder.encode(person.name, forKey: "name") | |
aCoder.encode(person.age, forKey: "age") | |
} | |
} | |
} | |
let person = Person(name: "Foo", age: 10) | |
let helper = Person.Helper(person: person) | |
let data = NSKeyedArchiver.archivedData(withRootObject: helper) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Person: Codable { | |
enum Child: String, Codable { | |
case son, daughter | |
} | |
let name: String | |
let age: Int | |
let children: [Child] | |
} | |
let person = Person(name: "Foo", age: 10, children: [.son, .daughter]) | |
let data = try? JSONEncoder().encode(person) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// A type that can convert itself into and out of an external representation. | |
public typealias Codable = Decodable & Encodable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let person = try? JSONDecoder().decode(Person.self, from: data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let data = NSKeyedArchiver.archivedData(withRootObject: helper) // 299 bytes | |
let data = try? JSONEncoder().encode(person) // 53 bytes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment