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. Motivation / Concept

  3. Protocol Buffers (Apple) https://github.com/apple/swift-protobuf

  4. Protocol buffers are a language-neutral, platform-neutral extensible mechanism for serializing

    structured data. Protocol Buffers (Google) https://developers.google.com/protocol-buffers/
  5. 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
  6. Value types + Serialize + Storing data on device Pencil

  7. ✏… Read/Write struct data on device more easily. Pencil

  8. Sample Code

  9. 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)
  10. Read Standard Values (Int) // read from file path let

    num = Int.value(from: storedURL)
  11. 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)
  12. Read Standard Values ([Int]) // read from file path let

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

    Int16, Int32, Int64 UInt8, UInt16, UInt32, UInt64
  14. 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") } }
  15. 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)
  16. Read Custom Struct // read from file path let sample

    = Sample.value(from: storedURL)
  17. 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)
  18. Read Custom Struct ([Sample]) // read from file path let

    samples = [Sample].value(from: storedURL)
  19. Example

  20. https://arxiv.org

  21. 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 { // … } // … }
  22. 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: "") ) } // … }
  23. Implementation

  24. Data Structure Format identifier header body

  25. Format identifier header body length + type_name (UInt8) Data Structure

  26. Data Structure (Int) identifier header body length 6 → <03496e74

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

    696e6706 0070656e 63696c> <06> <53747269 6e67> + type_name <0600> <70656e63 696c>
  28. 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>
  29. 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>
  30. 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>
  31. and more.

  32. I’m waiting for your Issue, PR and Star