質問編集履歴

2

persistentStoreCoordinatorの詳細を追記

2018/04/05 01:38

投稿

ttwiita
ttwiita

スコア16

test CHANGED
File without changes
test CHANGED
@@ -35,3 +35,91 @@
35
35
  swift 2.3 -> swift 4
36
36
 
37
37
  OS: iOS
38
+
39
+
40
+
41
+
42
+
43
+ ## swift 2.3の persistentStoreCoordinator
44
+
45
+
46
+
47
+ ```
48
+
49
+ lazy var managedObjectModel: NSManagedObjectModel = {
50
+
51
+ // 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.
52
+
53
+ let modelURL = NSBundle.mainBundle().URLForResource("db", withExtension: "momd")!
54
+
55
+ return NSManagedObjectModel(contentsOfURL: modelURL)!
56
+
57
+ }()
58
+
59
+
60
+
61
+ lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
62
+
63
+ // 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.
64
+
65
+
66
+
67
+ let options = [
68
+
69
+ NSInferMappingModelAutomaticallyOption: true,
70
+
71
+ NSMigratePersistentStoresAutomaticallyOption: true
72
+
73
+ ]
74
+
75
+
76
+
77
+ // Create the coordinator and store
78
+
79
+ let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
80
+
81
+ let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("CoreData.sqlite")
82
+
83
+ var failureReason = "There was an error creating or loading the application's saved data."
84
+
85
+ do {
86
+
87
+ //try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
88
+
89
+ try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: options)
90
+
91
+ } catch {
92
+
93
+ // Report any error we got.
94
+
95
+ var dict = [String: AnyObject]()
96
+
97
+ dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
98
+
99
+ dict[NSLocalizedFailureReasonErrorKey] = failureReason
100
+
101
+
102
+
103
+ dict[NSUnderlyingErrorKey] = error as NSError
104
+
105
+ let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
106
+
107
+ // Replace this with code to handle the error appropriately.
108
+
109
+ // 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.
110
+
111
+ NSLog("Unresolved error (wrappedError), (wrappedError.userInfo)")
112
+
113
+ abort()
114
+
115
+ }
116
+
117
+
118
+
119
+ return coordinator
120
+
121
+ }()
122
+
123
+
124
+
125
+ ```

1

環境の情報を追記

2018/04/05 01:38

投稿

ttwiita
ttwiita

スコア16

test CHANGED
File without changes
test CHANGED
@@ -27,3 +27,11 @@
27
27
 
28
28
 
29
29
  Bundle Identifireが同じでもこのようなことはできないのでしょうか。
30
+
31
+
32
+
33
+ ## 環境
34
+
35
+ swift 2.3 -> swift 4
36
+
37
+ OS: iOS