• Stars
    star
    90
  • Rank 369,088 (Top 8 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Super lightweight DB written in Swift.

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.

More Repositories

1

View2ViewTransition

Custom interactive view controller transition from one view to another view.
Swift
851
star
2

LoopedContentsView

View to display looped contents.
Swift
53
star
3

BottomSheetController

Component containing supplementary content that are anchored to the bottom of the screen.
Swift
39
star
4

MetalAR

AR application sample using Metal without SceneKit.
Swift
34
star
5

Monaka

Monaka convert custom struct to NSData.
Swift
22
star
6

Storages

Browse local storages of your applications.
Swift
22
star
7

pip-exercise

Some examples for iOS Picture-in-Picture.
21
star
8

CallStackSymbols

Get formatted call stack symbols.
Swift
13
star
9

100000-particles

Sample implementation for one hundred thousand particles simulated and drawn by Metal.
Swift
9
star
10

LayoutXML

Android styled XML template engine for iOS written in Swift.
Swift
7
star
11

Symbolicator

Convert numeric addresses to symbols with callStackSymbols and dSYM.
Swift
7
star
12

XMLLayouts

Layout system using XML for ios
Objective-C
6
star
13

NDISwiftPackage

Sample project with local swift package linked NDI SDK.
Swift
5
star
14

struct-archiver

Archive struct and fundamental values into NSData and unarchive.
Swift
5
star
15

MeasureAccelerates

Sample library to measure performance of calculation with Accelerate Framework.
Swift
5
star
16

metal-drawings

Swift
3
star
17

MPSTrainingChallenge

Challenge to train neural network using MetalPerformanceShaders Framework.
Objective-C
3
star
18

FrameMint

Make video frames in 420YpCbCr8BiPlanarFullRange from view representation of UIKit or SwiftUI.
Swift
2
star
19

KeyboardSimulator

Application to simulate keyboard inputs at intervals for macOS.
Swift
2
star
20

pip-battery-drain

Reproduce PiP Battery Drain
Swift
2
star
21

BluetoothRSASample

RSA通信の実感サンプル
Objective-C
1
star
22

hexadecimals

Convert hexadecimal number to some different radix numbers
Swift
1
star
23

InferenceVowel

Demo Application for Inference of vowel
Swift
1
star
24

PHHashService

Calculate hash for all local images and store them.
Objective-C
1
star
25

ObservableAppGroups

Observable file changes in app groups container with Combine.
Swift
1
star
26

Files

Framework to easily browse, observe and control local storage.
Swift
1
star