Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. import UIKit
  2. import CoreData
  3.  
  4. @UIApplicationMain
  5. class AppDelegate: UIResponder, UIApplicationDelegate {
  6.  
  7. var window: UIWindow?
  8.  
  9.  
  10. func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  11. // Override point for customization after application launch.
  12. return true
  13. }
  14.  
  15. func applicationWillResignActive(application: UIApplication) {
  16. // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  17. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
  18. }
  19.  
  20. func applicationDidEnterBackground(application: UIApplication) {
  21. // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  22. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  23. }
  24.  
  25. func applicationWillEnterForeground(application: UIApplication) {
  26. // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
  27. }
  28.  
  29. func applicationDidBecomeActive(application: UIApplication) {
  30. // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  31. }
  32.  
  33. func applicationWillTerminate(application: UIApplication) {
  34. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  35. // Saves changes in the application's managed object context before the application terminates.
  36. self.saveContext()
  37. }
  38.  
  39. // MARK: - Core Data stack
  40.  
  41. lazy var applicationDocumentsDirectory: NSURL = {
  42. // The directory the application uses to store the Core Data store file. This code uses a directory named "com.ShreyasPapinwar.Shreyas_Papinwar" in the application's documents Application Support directory.
  43. let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
  44. return urls[urls.count-1]
  45. }()
  46.  
  47. lazy var managedObjectModel: NSManagedObjectModel = {
  48. // 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.
  49. let modelURL = NSBundle.mainBundle().URLForResource("Shreyas_Papinwar", withExtension: "momd")!
  50. return NSManagedObjectModel(contentsOfURL: modelURL)!
  51. }()
  52.  
  53. lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
  54. // 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.
  55. // Create the coordinator and store
  56. var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
  57. let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("Shreyas_Papinwar.sqlite")
  58. var error: NSError? = nil
  59. var failureReason = "There was an error creating or loading the application's saved data."
  60. //this line has error - extra argument error in call
  61. if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
  62. coordinator = nil
  63. // Report any error we got.
  64. var dict = [String: AnyObject]()
  65. dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
  66. dict[NSLocalizedFailureReasonErrorKey] = failureReason
  67. dict[NSUnderlyingErrorKey] = error
  68. error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
  69. // Replace this with code to handle the error appropriately.
  70. // 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.
  71. NSLog("Unresolved error (error), (error!.userInfo)")
  72. abort()
  73. }
  74.  
  75. return coordinator
  76. }()
  77.  
  78. lazy var managedObjectContext: NSManagedObjectContext? = {
  79. // 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.
  80. let coordinator = self.persistentStoreCoordinator
  81. if coordinator == nil {
  82. return nil
  83. }
  84. var managedObjectContext = NSManagedObjectContext()
  85. managedObjectContext.persistentStoreCoordinator = coordinator
  86. return managedObjectContext
  87. }()
  88.  
  89. // MARK: - Core Data Saving support
  90.  
  91. func saveContext () {
  92. //line of error begins
  93. if let moc = self.managedObjectContext {
  94. var error: NSError? = nil
  95. if moc.hasChanges && !moc.save(error) {
  96. // Replace this implementation with code to handle the error appropriately.
  97. // 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.
  98. NSLog("Unresolved error (error), (error!.userInfo)")
  99. abort()
  100. }
  101. }
  102. }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement