Search code examples
swiftobjective-cswift5

How to pass a dictionary parameters with '&' to Objective-C method in Swift


I have a Objective-C library (and my project is Swift source). I'm trying to use the library's methods.

there's a method like below

- (int)recognize:(NSDictionary **)dictionaryResult;

In ObjC, simply call it like

NSDictionary *resultDictionary = [[NSDictionary alloc] init];
nRet = [crControl recognize:&resultDictionary];

However in Swift, when I call the method like below, an error occurs

var dictionary = NSDictionary.init()
nRet = crControl.recognize(&dictionary)

==================

The error message:

Cannot convert value of type 'AutoreleasingUnsafeMutablePointer' to expected argument type 'AutoreleasingUnsafeMutablePointer<NSDictionary?>'

How can I pass a empty dictionary with '&' to ObjC methods in Swift?


Solution

  • The translation to swift seems to have translated the parameter type to:

    AutoreleasingUnsafeMutablePointer<NSDictionary?>
    

    Note that it's a pointer to an optional NSDictionary, so you should declare the argument as an optional too:

    var dictionary: NSDictionary? = .init()