sschale
sschale

Reputation: 5188

Swift 2 Handling of Multi-Level Objective C Error Passing

I'm using Encrypted Core Data and want to follow an error on initialization. The Objective C header declares:

+(NSPersistentStoreCoordinator *)makeStoreWithStructOptions:(EncryptedStoreOptions *) options managedObjectModel:(NSManagedObjectModel *)objModel error:(NSError * __autoreleasing*)error;

However, the Swift bridging header interprets that as:

public class func makeStoreWithStructOptions(options: UnsafeMutablePointer<EncryptedStoreOptions>, managedObjectModel objModel: NSManagedObjectModel!, error: ()) throws -> NSPersistentStoreCoordinator

Note that it is requesting an object of type () instead of an NSError.

I have attempted to do:

var error: NSError? = nil
EncryptedStore.makeStoreWithOptions(options, managedObjectModel: myModel, error: &error)

But that results in an error saying the type is incorrect (and can't use the &)

If I instead try (hah!) this syntax, using an empty closure as a placeholder for its odd request:

do {
      try EncryptedStore.makeStoreWithOptions(options, managedObjectModel: myModel, error: {}())
    }catch{
      print(error)
    }

It compiles, but when an error is generated, it is not caught, though ECD does log the error for me (but that doesn't help). Attempting to eliminate , error: {}() from the function call returns a warning that the function now doesn't throw, which still doesn't help me.

The Objective C code that is generating the error is:

[persistentCoordinator addPersistentStoreWithType:EncryptedStoreType configuration:nil URL:databaseURL
        options:options error:error];

if (*error)
{
    NSLog(@"Unable to add persistent store.");
    NSLog(@"Error: %@\n%@\n%@", *error, [*error userInfo], [*error localizedDescription]);
}

return persistentCoordinator;

What is the correct syntax to capture the error in this case?

Upvotes: 0

Views: 80

Answers (0)

Related Questions