当前位置:   article > 正文

13.Swift CoreData_swift codedata

swift codedata

13.Swift CoreData


在Android中,一想到要操作SQLite,就觉得很繁琐;但是IOS中,有一套非常成熟的数据库API(接口)和GUI(图形用户界面),这样大大提升了用户体验,虽然,我们开发者,但是不可否认的是,我们也是一种另类的用户,一看到GUI,很多人可能更偏爱于IOS开发。

IOS数据库数据的增、删、改、查,其实也就是对NSManagedObjectContext的增、删、改、查。

项目适配CodeData

其实真的很简单,只需要你勾上User Core Data这个选项即可。

13-1

然后你会看到项目中的AppDelegate.swift文件多了不少东西。其中也包括如下:

    lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
        // The persistent store coordinator for the application. This implementation creates and returns 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 store
        let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
        let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite")

        // 打印数据库文件存放目录
        NSLog("\(url)")

        var failureReason = "There was an error creating or loading the application's saved data."
        do {
            try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
        } catch {
            // Report any error we got.
            var dict = [String: AnyObject]()
            dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
            dict[NSLocalizedFailureReasonErrorKey] = failureReason

            dict[NSUnderlyingErrorKey] = error as NSError
            let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/815006
推荐阅读
相关标签
  

闽ICP备14008679号