SwCD 0.1.3

SwCD 0.1.3

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最新发布2016年1月
SPM支持 SPM

airin 维护。



SwCD 0.1.3

  • 作者:
  • Airin

轻量级的 CoreData 库,用 Swift 编写。

要求

  • Xcode 7.1+
  • iOS 8.0+
  • Swift 2.1
  • CocoaPods 0.36+

在 Swift 中使用 CoreData

请参阅。

http://jamesonquave.com/blog/core-data-in-swift-tutorial-part-1/


Because of the way Swift modules work, we need to make one modification to the core data model.
In the field “Class” under the data model inspector for our entity, LogItem,
we need to specify the project name as a prefix to the class name.
So instead of just specifying “LogItem” as the class,
it needs to say “MyLog.LogItem”, assuming your app is called “MyLog”.

用法

设置

// DataModel is your dataModel (DataModel.xcdatamodeld)
SwCD.setup("DataModel", dbRootDirPath: nil, dbDirName: nil, dbName: nil)

创建实体

// Item is NSManagedObject Subclass
let entity = SwCD.createEntity(Item.self)

插入

SwCD.insert(Item.self, entities: [entity], completion: { success, error in
  // after call saveWithBlockAndWait function
  if success == true {
      // do something
  } else {
      printlin(error)
  }
})

查找

let predicate = NSPredicate(format: "name == %@", argumentArray: ["bobo james"])

// results is [Item]
let results = SwCD.find(Item.self, predicate: predicate, sortDescriptors: nil, fetchLimit: nil)

查找所有

// results is [Item]
let results = SwCD.all(Item.self, sortDescriptors: nil)

查找第一个

// results is Item?
let result = SwCD.findFirst(Item.self, attribute: "identifier == %@", values: ["1"])

更新

// entity is Item?
var entity = SwCD.findFirst(Item.self, attribute: "identifier == %@", values: ["1"])
entity.name = "after"

SwCD.update(Item.self, entities: [entity], completion: nil)

删除

// entity is Item?
var entity = SwCD.findFirst(Item.self, attribute: "identifier == %@", values: ["1"])

SwCD.delete(Item.self, entities: [entity], completion: nil)

删除所有

// Item is NSManagedObject Subclass
SwCD.deleteAll(Item.self, completion: nil)

安装

许可证

MIT 许可协议。有关更多信息,请参阅 LICENSE 文件。