Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Library - Pencil

naru-jpn
November 29, 2016

Swift Library - Pencil

Read/Write struct data on device more easily.

naru-jpn

November 29, 2016
Tweet

More Decks by naru-jpn

Other Decks in Technology

Transcript

  1. Pencil Swift Library Naruki Chigira - Timers inc. GitHub: naru-jpn,

    Twitter: @naruchigi https://github.com/naru-jpn/pencil
  2. Protocol buffers are a language-neutral, platform-neutral extensible mechanism for serializing

    structured data. Protocol Buffers (Google) https://developers.google.com/protocol-buffers/
  3. 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
  4. 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)
  5. 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)
  6. Read Standard Values ([Int]) // read from file path let

    nums = [Int].value(from: storedURL)
  7. Standard Values Int UInt Float Double String Array Dictionary Int8,

    Int16, Int32, Int64 UInt8, UInt16, UInt32, UInt64
  8. 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") } }
  9. 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)
  10. 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)
  11. Read Custom Struct ([Sample]) // read from file path let

    samples = [Sample].value(from: storedURL)
  12. 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 { // … } // … }
  13. 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: "") ) } // … }
  14. Data Structure (Int) identifier header body length 6 → <03496e74

    06000000 00000000> <03> <496e74> + type_name <> <06000000 00000000>
  15. Data Structure (String) identifier header body length "pencil" → <06537472

    696e6706 0070656e 63696c> <06> <53747269 6e67> + type_name <0600> <70656e63 696c>
  16. 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>
  17. 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>
  18. 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>