From 4407391378e654713534040e1d421f276b6d7e04 Mon Sep 17 00:00:00 2001 From: Scott Levie Date: Wed, 7 Nov 2018 14:02:47 -0700 Subject: [PATCH] EncryptedStore: Fixed not throwing error in Swift MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • When an Objective-C method ends with a parameter that is a pointer to an error, it is imported to Swift as a method that throws. However, the error will only be thrown if the return value is either nil or false. All the public methods ending with an error pointer parameter were checked for this flaw. Only these three needed correction: + (NSPersistentStoreCoordinator *)coordinator:byAddingStoreAtURL:configuration:options:error: + (NSPersistentStoreCoordinator *)makeStoreWithOptions:managedObjectModel:error: + (NSPersistentStoreDescription *)makeDescriptionWithOptions:configuration:error: --- Incremental Store/EncryptedStore.m | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/Incremental Store/EncryptedStore.m b/Incremental Store/EncryptedStore.m index aa7a382..4edb5e0 100755 --- a/Incremental Store/EncryptedStore.m +++ b/Incremental Store/EncryptedStore.m @@ -282,10 +282,9 @@ + (NSPersistentStoreCoordinator *)makeStoreWithOptions:(NSDictionary *)options m persistentCoordinator = [self coordinator:persistentCoordinator byAddingStoreAtURL:databaseURL configuration:nil options:options error:error]; - if (*error) - { - NSLog(@"Unable to add persistent store."); - NSLog(@"Error: %@\n%@\n%@", *error, [*error userInfo], [*error localizedDescription]); + if (*error) { + // Returning nil will cause the error object to be thrown as intended if this method is called in Swift + return nil; } return persistentCoordinator; @@ -297,7 +296,12 @@ + (NSPersistentStoreCoordinator *)coordinator:(NSPersistentStoreCoordinator *)co } [coordinator addPersistentStoreWithType:EncryptedStoreType configuration:configuration URL:url options:options error:error]; - + + if (*error) { + // Returning nil will cause the error object to be thrown as intended if this method is called in Swift + return nil; + } + return coordinator; } @@ -305,6 +309,12 @@ + (NSPersistentStoreDescription *)makeDescriptionWithOptions:(NSDictionary *)opt NSPersistentStoreDescription *description = [NSPersistentStoreDescription new]; EncryptedStoreFileManager *fileManager = [options objectForKey:self.class.optionFileManager] ?: [EncryptedStoreFileManager defaultManager]; [fileManager setupDatabaseWithOptions:options error:error]; + + if (*error) { + // Returning nil will cause the error object to be thrown as intended if this method is called in Swift + return nil; + } + description.type = self.optionType; description.URL = fileManager.databaseURL; description.configuration = configuration;