CocoaPods trunk is moving to be read-only. Read more on the blog, there are 14 months to go.
| TestsTested | ✓ |
| LangLanguage | SwiftSwift |
| License | MIT |
| ReleasedLast Release | Feb 2018 |
| SPMSupports SPM | ✓ |
Maintained by naru.
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.
CocoaPods
pod 'pencil'
Swift Package Manager
Compatible.
Manually
Copy all *.swift files contained in Sources directory into your project.
Int value into file on device and read it.String value into file named for each textfield and read it.// (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)let text: String = "Pencil store value easily."
text.write(to: storedURL)
// ...
let stored: String? = String.value(from: storedURL)let nums: [Int] = [2016, 11, 28]
nums.write(to: storedURL)
// ...
let stored: [Int]? = [Int].value(from: storedURL)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 Bool, Float, Double, Date, Int8, Int16, Int32, Int64, UInt, UInt8, UInt16, UInt32 and UInt64.
Type of raw value should conform ReadWriteElement and add ReadWriteElement for enum.
enum Sample: Int, ReadWriteElement {
case one = 1
case two = 2
}
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)Sample in this case).CustomReadWriteElement.static func read and return Sample or nil.
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
}
}
}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)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)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")
)
}
}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.
Pencil is released under the MIT license. See LICENSE for details.