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...
ObjectBox Swift - persisting your Swift objects superfast and simple
ObjectBox Swift - persisting your Swift objects superfast and simple

ObjectBox Swift ObjectBox is a superfast, light-weight object persistence framework. This Swift API seamlessly persists objects on-device for iOS and

Shows the issue with swift using an ObjC class which has a property from a swift package.

SwiftObjCSwiftTest Shows the issue with swift using an ObjC class which has a property from a swift package. The Swift class (created as @objc derived

Ios-App-ication-Swift - A simple iOS application made in Xcode using Swift
Ios-App-ication-Swift - A simple iOS application made in Xcode using Swift

📱 iPhone Calculator A simple iOS application made in Xcode using Swift. This ap

Save-the-dot-project-swift - Save the dot project with swift
Save-the-dot-project-swift - Save the dot project with swift

Save the Dot Apple introduced UIViewPropertyAnimator for iOS 10. We can use this

The Swift Package Index is the place to find Swift packages!
The Swift Package Index is the place to find Swift packages!

The Swift Package Index helps you make better decisions about the dependencies you use in your apps. The Swift Package Index is a search engine for pa

A stand-alone Swift wrapper around the mongo-c client library, enabling access to MongoDB servers.
A stand-alone Swift wrapper around the mongo-c client library, enabling access to MongoDB servers.

This package is deprecated in favour of the official Mongo Swift Driver. We advise users to switch to that pack

Elegant library to manage the interactions between view and model in Swift
Elegant library to manage the interactions between view and model in Swift

An assistant to manage the interactions between view and model ModelAssistant is a mediator between the view and model. This framework is tailored to

CRUD is an object-relational mapping (ORM) system for Swift 4+.

CRUD is an object-relational mapping (ORM) system for Swift 4+. CRUD takes Swift 4 Codable types and maps them to SQL database tables. CRUD can create tables based on Codable types and perform inserts and updates of objects in those tables. CRUD can also perform selects and joins of tables, all in a type-safe manner.

Solutions to LeetCode by Swift
Solutions to LeetCode by Swift

LeetCode by Swift LeetCode Online Judge is a website containing many algorithm questions. Most of them are real interview questions of Google, Faceboo

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
A lightweight wrapper over UserDefaults/NSUserDefaults with an additional layer of AES-256 encryption

SecureDefaults for iOS, macOS Requirements • Usage • Installation • Contributing • Acknowledgments • Contributing • Author • License SecureDefaults is

Victor Peschenkov 216 Dec 22, 2022
CoreXLSX is a Excel spreadsheet (XLSX) format parser written in pure Swift

CoreXLSX Excel spreadsheet (XLSX) format parser written in pure Swift CoreXLSX is a library focused on representing the low-level structure of the XML

null 684 Dec 21, 2022
CoreData/Realm sweet wrapper written in Swift

What is SugarRecord? SugarRecord is a persistence wrapper designed to make working with persistence solutions like CoreData in a much easier way. Than

Modo 2.1k Dec 9, 2022
💾 Safe, statically-typed, store-agnostic key-value storage written in Swift!

Storez ?? Safe, statically-typed, store-agnostic key-value storage Highlights Fully Customizable: Customize the persistence store, the KeyType class,

Kitz 67 Aug 7, 2022
CRRateLimitTester - Simple Clash Royale Rate Limit Tester Written Using HummingBird and Swift

CRRateLimitTester Simple Clash Royale Rate Limit Tester Written Using HummingBir

Mahdi Bahrami 0 Jan 16, 2022
HoneycombView is the iOS UIView for displaying like Honeycomb layout written by swift

HoneycombView iOS UIView for Honeycomb layout include with photoBrowser. Requirements iOS 8.0+ Swift 2.0+ ARC ##Installation ####CocoaPods HoneycombVi

keishi suzuki 200 May 16, 2022
Easy and beautiful way for a user to pick content, files or images. Written in Objective C

##JVTImageFilePicker Description ImageFilesPicker Is a butifuly designed UICompenent for uploading content Preview from recent camera roll Live Camera

Matan Abravanel 60 Jun 19, 2022
A fast, pure swift MongoDB driver based on Swift NIO built for Server Side Swift

A fast, pure swift MongoDB driver based on Swift NIO built for Server Side Swift. It features a great API and a battle-tested core. Supporting both MongoDB in server and embedded environments.

null 646 Dec 10, 2022
SQLite.swift - A type-safe, Swift-language layer over SQLite3.

SQLite.swift provides compile-time confidence in SQL statement syntax and intent.

Stephen Celis 8.7k Jan 3, 2023
🧡 SQLiteOrm-Swift is an ORM library for SQLite3 built with Swift 5

?? Easy to use SQLite ORM library written with Swift

Yevgeniy Zakharov 25 Oct 6, 2022