coreData数据最终的存储类型可以是:SQLite数据库、XML、二进制、内存里、自定义的数据类型。
和SQLite区别:只能取出整个实体记录,然后分解,之后才能得到实体的某个属性。
1、创建工程勾选use coreData选项。
AppDelete.swift中自动生成一些方法:
1 // MARK: - Core Data stack 2 3 lazy var applicationDocumentsDirectory: NSURL = { 4 // The directory the application uses to store the Core Data store file. This code uses a directory named "com.LMY.LMYCoreData" in the application's documents Application Support directory. 5 let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) 6 return urls[urls.count-1] 7 }() 8 9 lazy var managedObjectModel: NSManagedObjectModel = { 10 // 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. 11 let modelURL = NSBundle.mainBundle().URLForResource("LMYCoreData", withExtension: "momd")! 12 return NSManagedObjectModel(contentsOfURL: modelURL)! 13 }() 14 15 lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = { 16 // 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. 17 // Create the coordinator and store 18 let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) 19 let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite") 20 var failureReason = "There was an error creating or loading the application's saved data." 21 do { 22 try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil) 23 } catch { 24 // Report any error we got. 25 var dict = [String: AnyObject]() 26 dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" 27 dict[NSLocalizedFailureReasonErrorKey] = failureReason 28 29 dict[NSUnderlyingErrorKey] = error as NSError 30 let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict) 31 // Replace this with code to handle the error appropriately. 32 // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 33 NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)") 34 abort() 35 } 36 37 return coordinator 38 }() 39 40 lazy var managedObjectContext: NSManagedObjectContext = { 41 // 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. 42 let coordinator = self.persistentStoreCoordinator 43 var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType) 44 managedObjectContext.persistentStoreCoordinator = coordinator 45 return managedObjectContext 46 }() 47 48 // MARK: - Core Data Saving support 49 50 func saveContext () { 51 if managedObjectContext.hasChanges { 52 do { 53 try managedObjectContext.save() 54 } catch { 55 // Replace this implementation with code to handle the error appropriately. 56 // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 57 let nserror = error as NSError 58 NSLog("Unresolved error \(nserror), \(nserror.userInfo)") 59 abort() 60 } 61 } 62 } 63 64 }