swift中使用CoreData实现增删查改

1.建立CoreData项目

创建项目时勾选 User Core Data

2.创建实体(Entity)

创建项目后,可以看到目录中有一个.xcdatamodeld文件。打开它,点击下方Add Entity按钮即可创建新的实体。这里,我创建了Student实体,两个属性分别是创建时间data和名字name。完成后点击右侧Show the Data Model Inspector按钮,里面有Name和Class两个选项,分别是实体名称和要生成的类名。 完成后,选择菜单Editor–>Create NSManagedObject Subclass…即可完成创建StudentManagedObject实体。

代码如下:

import Foundationimport CoreData@objc(StudentManagedObject)class StudentManagedObject: NSManagedObject {@NSManaged var date: NSDate@NSManaged var name: String}3.创建模型

既然有了实体,为什么还要创建模型呢?这是基于分层设计考虑的,上面一步生产的实体是NSManagedObject的子类,从层级看是属于持久层。持久层和业务层,展示层分离,在项目逻辑比较复杂的时候会体现出很大的优势。因此,我这里再创建一个继承NSObject的StudentModel模型,属性和StudentManagedObject完全一致。

代码如下:

import UIKitclass StudentModel: NSObject {: NSDate?}4.创建BaseDao基类

CoreData项目创建的时候,在AppDelegate中会自动生成四个参数: applicationDocumentsDirectory 这个参数是数据库文件的路径 managedObjectModel 数据库所有实体或数据结构,包含各实体的定义信息 persistentStoreCoordinator 相当于数据库的连接 managedObjectContext 被管理对象上下文,可以在上下文中实现查找,删除,插入等操作,最后需要同步 虽然看上去很复杂,用到的主要还是最后一个managedObjectContext。

默认这几个参数是在AppDelegeta中,为了构建一个统一的数据库访问接口,我们需要创建一个BaseDao类,所有然后把四个参数拷贝过去,所有数据库访问Dao都继承这个基类,这样就可以方便调用。代码如下:

import UIKitimport CoreDataclass BaseDao: NSObject {// MARK: – Core Data stacklazy var applicationDocumentsDirectory: NSURL = {// The directory the application uses to store the Core Data store file. This code uses a directory named “com.worthy.CoreDataDemo” in the application’s documents Application Support directory.let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)return urls[urls.count-1] as! NSURL}()lazy var managedObjectModel: NSManagedObjectModel = {// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.let modelURL = NSBundle.mainBundle().URLForResource(“CoreDataDemo”, withExtension: “momd”)!return NSManagedObjectModel(contentsOfURL: modelURL)!}()lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.// Create the coordinator and storevar coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)let url = (“CoreDataDemo.sqlite”)var error: NSError? = nilvar failureReason = “There was an error creating or loading the application’s saved data.”if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {coordinator = nil// Report any error we got.var dict = [String: AnyObject]()dict[NSLocalizedDescriptionKey] = “Failed to initialize the application’s saved data”dict[NSLocalizedFailureReasonErrorKey] = failureReasondict[NSUnderlyingErrorKey] = errorerror = NSError(domain: “YOUR_ERROR_DOMAIN”, code: 9999, userInfo: dict)(“Unresolved error \(error), \(error!.userInfo)”)abort()}return coordinator}()lazy var managedObjectContext: NSManagedObjectContext? = {// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.let coordinator = coordinator == nil {return nil}var managedObjectContext = NSManagedObjectContext()managedObjectContext.persistentStoreCoordinator = coordinatorreturn managedObjectContext}()}5.创建数据访问对象StudentDao孤独是为了孤独背后的解脱,孤独的过程,就是一个寻找真爱的过程。

swift中使用CoreData实现增删查改

相关文章:

你感兴趣的文章:

标签云: