Super lightweight DB written in Swift.

Overview

pencil_logo

platform-ios carthage-compatible cocoapods-compatible swift-3.0 MIT

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.

You might also like...
NSCoding's counterpart for Swift structs.
NSCoding's counterpart for Swift structs.

Dekoter Why You Might Be Interested How Much Familiar It Feels One More Example What We've Learned from It Features Save an Object to UserDefaults Arc

Algorithms and data structures in Swift, with explanations!
Algorithms and data structures in Swift, with explanations!

Welcome to the Swift Algorithm Club! Here you'll find implementations of popular algorithms and data structures in everyone's favorite new language Sw

A Distributed Value Store in Swift.
A Distributed Value Store in Swift.

Impeller is a Distributed Value Store (DVS) written in Swift. It was inspired by successful Distributed Version Control Systems (DVCSes) like Git and

🦀Amazingly incredible extraordinary lightning fast diffing in Swift
🦀Amazingly incredible extraordinary lightning fast diffing in Swift

DeepDiff ❤️ Support my apps ❤️ Push Hero - pure Swift native macOS application to test push notifications PastePal - Pasteboard, note and shortcut man

Swift library to generate differences and patches between collections.

Differ Differ generates the differences between Collection instances (this includes Strings!). It uses a fast algorithm (O((N+M)*D)) to do this. Featu

A Swift probability and statistics library

Probably Probably is a set of Swift structures for computing the probability and cumulative distributions of different probablistic functions. Right n

The simplest abstraction to synchronize local data with remote source. For iOS, wirtten in swift.

Purpose The simplest abstraction to synchronize local data with remote source. For iOS, written in swift. Overview Many applications uses remote serve

💻 A fast and flexible O(n) difference algorithm framework for Swift collection.
💻 A fast and flexible O(n) difference algorithm framework for Swift collection.

A fast and flexible O(n) difference algorithm framework for Swift collection. The algorithm is optimized based on the Paul Heckel's algorithm. Made wi

Simple implementations of various dynamic data structures in Swift.

SwiftDataStructures Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Installatio

Comments
  • Manual Installation

    Manual Installation

    CocoaPods and Carthage are awesome tools and make our life really easier, but there are some devs who still don't know how to use them.

    It would be cool to add the Manual installation guide in your README.md. You can take a look at my iOS Readme Template to see how you can do it.

    opened by lfarah 2
  • minium development target

    minium development target

    Thanks for awesome library!

    But, my product is able to build over iOS 9. Pencil's minimum deployment target is currently iOS 10.

    Please change deployment target to iOS 9.0!

    Note: If you want many users, you may change to iOS 8.

    opened by fromkk 1
Owner
Naruki Chigira
iOS application engineer.
Naruki Chigira
Commonly used data structures for Swift

Swift Collections is an open-source package of data structure implementations for the Swift programming language.

Apple 2.7k Jan 5, 2023
Fast sorted collections for Swift using in-memory B-trees

Fast Sorted Collections for Swift Using In-Memory B-Trees Overview Reference Documentation Optimizing Collections: The Book What Are B-Trees? Why In-M

null 1.3k Dec 20, 2022
Examples of commonly used data structures and algorithms in Swift.

Swift Structures This project provides a framework for commonly used data structures and algorithms written in a new iOS development language called S

Wayne Bishop 2.1k Dec 28, 2022
Simple diff library in pure Swift

Diff Simple diffing library in pure Swift. Installing You can use Carthage or Swift Package Manager to install Diff. Usage Start by importing the pack

Sam Soffes 120 Sep 9, 2022
A functional tool-belt for Swift Language similar to Lo-Dash or Underscore.js in Javascript

Dollar Dollar is a Swift library that provides useful functional programming helper methods without extending any built in objects. It is similar to L

Ankur Patel 4.2k Jan 4, 2023
Swift type modelling the success/failure of arbitrary operations.

Result This is a Swift µframework providing Result<Value, Error>. Result<Value, Error> values are either successful (wrapping Value) or failed (wrappi

Antitypical 2.5k Dec 26, 2022
Swift μ-framework for efficient array diffs and datasource adapters.

Buffer Swift μ-framework for efficient array diffs, collection observation and data source implementation. C++11 port here Installation cd {PROJECT_RO

Alex Usbergo 348 Aug 2, 2022
A Graph Data Structure in Pure Swift

SwiftGraph SwiftGraph is a pure Swift (no Cocoa) implementation of a graph data structure, appropriate for use on all platforms Swift supports (iOS, m

David Kopec 700 Dec 16, 2022
A Generic Priority Queue in Pure Swift

SwiftPriorityQueue SwiftPriorityQueue is a pure Swift (no Cocoa) implementation of a generic priority queue data structure, appropriate for use on all

David Kopec 350 Dec 22, 2022
A fast Swift diffing library.

HeckelDiff Pure Swift implementation of Paul Heckel's A Technique for Isolating Differences Between Files Features This is a simple diff algorithm tha

Matias Cudich 166 Oct 6, 2022