Adds a new persistent store of a specified type at a given location, and returns the new store.
SDKs
- iOS 3.0+
- macOS 10.4+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
Framework
- Core Data
Declaration
func addPersistentStore(ofType storeType: String, configurationName configuration: String?, at storeURL: URL?, options: [Any Hashable : Any]? = nil) throws -> NSPersistent Store
Parameters
storeType
A string constant (such as
NSSQLite
) that specifies the store type—see Persistent Store Types for possible values.Store Type configuration
The name of a configuration in the receiver's managed object model that will be used by the new store. The configuration can be
nil
, in which case no other configurations are allowed.storeURL
The file location of the persistent store.
options
A dictionary containing key-value pairs that specify whether the store should be read-only, and whether (for an XML store) the XML file should be validated against the DTD before it is read. For key definitions, see Store Options and Migration Options. This value may be
nil
.error
If a new store cannot be created, upon return contains an instance of
NSError
that describes the problem
Return Value
The newly-created store or, if an error occurs, nil
.
Discussion
Handling Errors in Swift:
In Swift, this method returns a nonoptional result and is marked with the throws
keyword to indicate that it throws an error in cases of failure.
You call this method in a try
expression and handle any errors in the catch
clauses of a do
statement, as described in Error Handling in The Swift Programming Language and About Imported Cocoa Error Parameters.