1

I need to pass a Dictionary to a Objective-C method in Swift. In Swift the code is like this:

let modelData: Dictionary<String, [Double]> = getModelData()
result = myChilitags.estimate3D(configAt: configFilePath, forModel: modelData);

(The configure file has nothing to do with this problem, just ignore it.)

I used a .h file:

@interface myChilitags : NSObject
+ (nonnull UIImage *)estimate3D:configAt:(NSString *)configFilePath forModel:(nonnull NSDictionary*) modelData;
@end

The question is that I need to do something with the modelData in the Objective-C method estimate3D but I don't know what to do after I passed the Dictionary value modelData to the method.

I tried to just print the modelData value but all that came out was:

1

I also tried to print the value in the Dictionary like:

std::cout << modelData["face001"] << std::endl;

I am pretty sure that there is a key "face001" in the dictionary but the result was still:

1

I know it must have something to do with NSDictionary and Dictionary but I just don't know what to do.

Zion
  • 1,562
  • 13
  • 32
Jerry Chang
  • 67
  • 1
  • 7
  • Possible duplicate: [How to call Objective-C code from Swift](https://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift) – Miket25 Dec 31 '17 at 02:38
  • 1
    I don't think this is a duplicate. OP's example is *Objective-C++*, not Objective-C – Michael Hulet Dec 31 '17 at 04:10

1 Answers1

0

First of all, a Dictionary in Swift is struct, an NSDictionary is class.
Objective-C is not type-safe so it doesn't show an error.
If you try to do the same in Swift, it will tell you
Cannot assign value of type '[String : [Double]]' to type 'NSDictionary'

let swiftDictionary = [String: [Double]]()
var nsDictionary = NSDictionary()
nsDictionary = swiftDictionary //shows error

So you have to convert the Swift dictionary to an NSDictionary.

let modelData: Dictionary<String, [Double]> = getModelData()
let nsModelData = NSDictionary(dictionary: modelData)
result = myChilitags.estimate3D(configAt: configFilePath, forModel: nsModelData);
florieger
  • 1,310
  • 12
  • 22