赞
踩
在Android中,一想到要操作SQLite,就觉得很繁琐;但是IOS中,有一套非常成熟的数据库API(接口)和GUI(图形用户界面),这样大大提升了用户体验,虽然,我们开发者,但是不可否认的是,我们也是一种另类的用户,一看到GUI,很多人可能更偏爱于IOS开发。
IOS数据库数据的增、删、改、查,其实也就是对NSManagedObjectContext的增、删、改、查。
其实真的很简单,只需要你勾上User Core Data
这个选项即可。
然后你会看到项目中的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",

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。