Share
Embed
Transcript
32 slides
-
Pencil Swift Library Naruki Chigira - Timers inc. GitHub: naru-jpn,
Twitter: @naruchigi https://github.com/naru-jpn/pencil -
Protocol buffers are a language-neutral, platform-neutral extensible mechanism for serializing
structured data. Protocol Buffers (Google) https://developers.google.com/protocol-buffers/ -
Protocol Buffers is a method of serializing structured data. It
is useful in developing programs to communicate with each other over a wire or for storing data. … The design goals for Protocol Buffers emphasized simplicity and performance. In particular, it was designed to be smaller and faster than XML. Protocol Buffers (Wikipedia) https://en.wikipedia.org/wiki/Protocol_Buffers -
Write Standard Values (Int) // get file URL guard let
storedURL = Directory.Documents?.append(path: "num.data") else { return } let num: Int = 2016 // write to file num.write(to: storedURL) -
Read Standard Values (Int) // read from file path let
num = Int.value(from: storedURL) -
Write Standard Values ([Int]) // get file URL guard let
storedURL = Directory.Documents?.append(path: "nums.data") else { return } let nums: Int = [2016, 11, 29] // write to file nums.write(to: storedURL) -
Read Standard Values ([Int]) // read from file path let
nums = [Int].value(from: storedURL) -
Standard Values Int UInt Float Double String Array Dictionary Int8,
Int16, Int32, Int64 UInt8, UInt16, UInt32, UInt64 -
Custom Struct // Custom Struct struct Sample: CustomReadWriteElement { //
Parameters let dictionary: [String: Int] let array: [Int]? let identifier: String // Function returns Sample from Components static var read: (Components) -> Sample? = { components in return Sample.init =<> components.component(for: "dictionary", defaultValue: ["a": 100]) -<> components.component(for: "array") -<> components.component(for: "identifier", defaultValue: "abc123") } } -
Write Custom Struct // get file URL guard let storedURL
= Directory.Documents?.append(path: "sample.data") else { return } let sample: Sample = Sample(dictionary: ["a": 2, "b": 5], array: [2, 3], identifier: “id") // write to file sample.write(to: storedURL) -
Read Custom Struct // read from file path let sample
= Sample.value(from: storedURL) -
Write Custom Struct ([Sample]) // get file URL guard let
storedURL = Directory.Documents?.append(path: "samples.data") else { return } let sample: Sample = Sample(dictionary: ["a": 2, "b": 5], array: [2, 3], identifier: “id”) let samples: [Samples] = [sample, sample, sample] // write to file samples.write(to: storedURL) -
Read Custom Struct ([Sample]) // read from file path let
samples = [Sample].value(from: storedURL) -
Entry struct Entry: Equatable, CustomDebugStringConvertible, CustomReadWriteElement { var id: String
= "" var updated: Double = NSDate(timeIntervalSince1970: 0.0).timeIntervalSince1970 var published: Double = NSDate(timeIntervalSince1970: 0.0).timeIntervalSince1970 var title: String = "" var summary: String = "" var authors: [Entry.Author] = [] var updatedDay: String = "" var publishedDay: String = "" var htmlLink: String = "" var pdfLink: String = "" var primaryCategory: String = "" struct Author: CustomDebugStringConvertible, CustomReadWriteElement { // … } // … } -
Entry.Author struct Author: CustomDebugStringConvertible, CustomReadWriteElement { var name: String =
"" var affiliation: String = "" static var read: (Components) -> Entry.Author? = { components in return Entry.Author( name: components.component(for: "name", defaultValue: ""), affiliation: components.component(for: "affiliation", defaultValue: "") ) } // … } -
Data Structure (Int) identifier header body length 6 → <03496e74
06000000 00000000> <03> <496e74> + type_name <> <06000000 00000000> -
Data Structure (String) identifier header body length "pencil" → <06537472
696e6706 0070656e 63696c> <06> <53747269 6e67> + type_name <0600> <70656e63 696c> -
Data Structure ([Int]) identifier header body length [1,2,3] → <0a417272
61793c49 6e743e03 000c0000 000c0000 000c0000 0003496e 74010000 00000000 0003496e 74020000 00000000 0003496e 74030000 00000000 00> <0a> <417272 61793c49 6e743e> + type_name <03000c00 00000c00 00000c00 0000> <03496e74 01000000 00000000 03496e74 02000000 00000000 03496e74 03000000 00000000> -
Data Structure ([Int]) identifier header body length [1,2,3] → <0a417272
61793c49 6e743e03 000c0000 000c0000 000c0000 0003496e 74010000 00000000 0003496e 74020000 00000000 0003496e 74030000 00000000 00> <0a> <417272 61793c49 6e743e> + type_name <03000c00 00000c00 00000c00 0000> <03496e74 01000000 00000000 03496e74 02000000 00000000 03496e74 03000000 00000000> -
Data Structure ([Int]) identifier header body length [1,2,3] → <0a417272
61793c49 6e743e03 000c0000 000c0000 000c0000 0003496e 74010000 00000000 0003496e 74020000 00000000 0003496e 74030000 00000000 00> <0a> <417272 61793c49 6e743e> + type_name <03000c00 00000c00 00000c00 0000> <03496e74 01000000 00000000 03496e74 02000000 00000000 03496e74 03000000 00000000>