How to turn off Core Data Write-Ahead logging in Swift using options dictionary?

后端 未结 3 965
你的背包
你的背包 2021-01-06 04:22

How do I turn off the SQLite Write ahead logging (WAL) in Core Data using Apples new programming language Swift?

In ObjC I used to pass in the key value pair @\"jour

3条回答
  •  攒了一身酷
    2021-01-06 05:19

    Swift dictionaries are strongly typed by default, but you can define the types your dictionary should accept.

    var options = Dictionary()
    options[NSMigratePersistentStoresAutomaticallyOption] = true
    options[NSInferMappingModelAutomaticallyOption] = true
    options["journal_mode"] = "DELETE"
    
    [storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                   configuration:nil
                                             URL:[self databaseURL]
                                         options:opitons
                                           error:&error]
    

    If you weren't sure what type of dictionary the function was expecting, just take a look at the function declaration :

    func addPersistentStoreWithType(_ storeType: String!,
       configuration configuration: String!,
           URL storeURL: NSURL!,
        options options: [NSObject : AnyObject]!,
            error error: AutoreleasingUnsafePointer) -> NSPersistentStore!
    

    It describes exactly the type of dictionary it expects -- Dictionary.

    In fact, any dictionary that is bridged from Objective-C will be typed this way. From the Apple Docs:

    Swift also automatically bridges between the Dictionary type and the NSDictionary class. When you bridge from an NSDictionary object to a Swift dictionary, the resulting dictionary is of type [NSObject: AnyObject].

    You can bridge any NSDictionary object to a Swift dictionary because all Objective-C objects are AnyObject compatible. Recall that an object is AnyObject compatible if it is an instance of an Objective-C or Swift class, or if it can be bridged to one. All NSDictionary objects can be bridged to Swift dictionaries, so the Swift compiler replaces the NSDictionary class with [NSObject: AnyObject] when it imports Objective-C APIs.

    Likewise, when you use a Swift class or protocol in Objective-C code, the importer remaps Objective-C compatible Swift dictionaries as NSDictionary objects.

提交回复
热议问题