铅笔 0.3.0

pencil 0.3.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2018年2月
SPM支持 SPM

naru 维护。



pencil 0.3.0

  • 作者
  • naru

推荐使用值类型,并把我们定义的标准的值,简单的结构化数据,应用状态等定义为结构体或枚举。Pencil使得存储这些值更加容易。

安装

CocoaPods

pod 'pencil'

Swift Package Manager

兼容。

手动

将包含在Sources目录中的所有*.swift文件复制到您的项目中。

推荐 / 示例

  • 类似于最后一次选中的应用中标签页的索引的应用状态。
    • 您可以将Int值写入设备上的文件并在那里读取它。
  • 最近用户插入的textfiled值。
    • 您可以将每个textfiled的名称写入文件并将该文件读取出来。
  • 没有使用任何数据库系统的结构化数据。
    • 您可以在设备上写入结构体值并在那里读取它。

使用

标准值

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 (包含可写值)

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

// ...

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

Dictionary (含字符串键的可写值)

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

// ...

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

其他标准可写和可读值包括BoolFloatDoubleDateInt8Int16Int32Int64UIntUInt8UInt16UInt32UInt64

枚举

定义可写和可读枚举

原始值类型应符合ReadWriteElement,并为枚举添加ReadWriteElement

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

写入文件 / 从文件路径读取

使用与标准值相同的方式进行读写。

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

// ...

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

自定义结构体

定义可写和可读的自定义结构体

  1. 定义自定义结构体(本例中命名为 Sample)。
  2. 遵循协议 CustomReadWriteElement
  3. 实现函数 static func read 并返回 Samplenil
    • 将每个参数与参数名一起应用。
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
    }
  }
}

写入文件 / 从文件路径读取

使用与标准值相同的方式进行读写。

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)

包含自定义结构体的复杂值

现在您可以写入和读取包含自定义结构体的复杂值。

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)

以默认参数读取结构体

定义具有默认参数的自定义结构体。如果所有属性都具有默认值或可选,则不需要使用 try

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 支持使用 Directory 类创建文件路径。

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

其他目录有 ApplicationsDemosDocumentationDocumentsAutosavedInformationCachesDownloads

示例

PencilExample

许可证

Pencil 是在 MIT 许可下发布的。有关详细信息,请参阅 LICENSE。