ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - naru-jpn/pencil: Super lightweight DB written in Swift.
Super lightweight DB written in Swift. Contribute to naru-jpn/pencil development by creating an account on GitHub.
Visit Site

GitHub - naru-jpn/pencil: Super lightweight DB written in Swift.

GitHub - naru-jpn/pencil: Super lightweight DB written in Swift.

Use of value types is recommended and we define standard values, simple structured data, application state and etc. as struct or enum. Pencil makes us store these values more easily.

Installation

Carthage

github "naru-jpn/Pencil"

CocoaPods

pod 'pencil'

Swift Package Manager

Compatible.

Manually

Copy all *.swift files contained in Sources directory into your project.

Recommeded / Example

  • Application state such as tab index application selected at last time.
    • You can write Int value into file on device and read it.
  • Recently user inserted textfield value.
    • You can write String value into file named for each textfield and read it.
  • Structured data without any DB system.
    • You can write struct values into file on device and read it.

Usage

Standard values

Int

// (create stored url)
guard let storedURL = Directory.Documents?.append(path: "int.data") else {
  return
}

let num: Int = 2016

// write
num.write(to: storedURL)

// ...

// read
let stored: Int? = Int.value(from: storedURL)

String

let text: String = "Pencil store value easily."
text.write(to: storedURL)

// ...

let stored: String? = String.value(from: storedURL)

Array (containing writable values)

let nums: [Int] = [2016, 11, 28]
nums.write(to: storedURL)

// ...

let stored: [Int]? = [Int].value(from: storedURL)

Dictionary (contaning writable values with string key)

let dictionary: [String: Int] = ["year": 2016, "month": 11, "day": 28]
dictionary.write(to: storedURL)

// ...

let stored: [String: Int]? = [String: Int].value(from: url)

Other standard writable and readable values are Set, Bool, Float, Double, Date, Int8, Int16, Int32, Int64, UInt, UInt8, UInt16, UInt32 and UInt64.

Enum

Define writable and readable enum

Type of raw value should conform ReadWriteElement and add ReadWriteElement for enum.

enum Sample: Int, ReadWriteElement {
  case one = 1
  case two = 2
}

write to file / read from file path

Read and write values by the same way with standard values.

let sample: Sample = .two
sample.write(to: storedURL)

// ...

let stored: Sample? = Sample.value(from: url)

Custom struct

Define writable and readable custom struct

  1. Define custom struct (named Sample in this case).
  2. Conform protocol CustomReadWriteElement.
  3. Implement function static func read and return Sample or nil.
  • Apply each parameters with parameter name.
struct Sample: CustomReadWriteElement {
    
  let dictionary: [String: Int]
  let array: [Int]?
  let identifier: String
    
  static func read(components: Components) -> Sample? {
    do {
      return try Sample(
        dictionary: components.component(for: "dictionary"),
        array:      components.component(for: "array"),
        identifier: components.component(for: "identifier")
      )
    } catch {
      return nil
    }
  }
}

write to file / read from file path

Read and write values by the same way with standard values.

let sample: Sample = Sample(dictionary: ["one": 2, "two": 5], array: [2, 3], identifier: "abc123")
sample.write(to: storedURL)

// ...

let stored: Sample? = Sample.value(from: url)

Complex values containing custom struct

You can now write and read complex values containing custom struct.

let sample: Sample = Sample(dictionary: ["one": 2, "two": 5], array: [2, 3], identifier: "abc123")
let samples: [Samples] = [sample, sample, sample]
samples.write(to: storedURL)

// ...

let stored: [Sample]? = [Sample].value(from: url)

Read struct with default parameters

Define custom struct with default parameters. You need not to try if all properties have default values or optional.

struct Sample: CustomReadWriteElement {
    
    let dictionary: [String: Int]
    let array: [Int]?
    let identifier: String
    
    static func read(components: Components) -> Sample? {      
        return Sample(
            dictionary: components.component(for: "dictionary", defaultValue: ["default": 100]),
            array:      components.component(for: "array"),
            identifier: components.component(for: "identifier", defaultValue: "default")
        )
    }
}

Create stored file path

Pencil support to create file path using class Directory.

// Create path ~/Documents/pencil.data
let url: URL? = Directory.Documents?.append(path: "pencil.data")

Other directories are Applications, Demos, Documentation, Documents, AutosavedInformation, Caches and Downloads.

Example

PencilExample

License

Pencil is released under the MIT license. See LICENSE for details.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory