1
votes

I am trying to convert a Dictionary to NSDictionary.

document = NSDictionary\<string, object\>(); // with some data of course

The way I am doing this is:

NSDictionary<NSString, NSObject> iosDoc = NSDictionary<NSString, NSObject>.FromObjectsAndKeys(
  document.Values.ToArray(),  
  document.Keys.ToArray()
);

And the error I get is the following:

Error CS1061: 'Dictionary<string, object>.ValueCollection' does not contain a definition for 'ToArray' and no accessible extension method 'ToArray' accepting a first argument of type 'Dictionary<string, object>.ValueCollection' could be found (are you missing a using directive or an assembly reference?) (CS1061)

Error CS1061: 'Dictionary<string, object>.KeyCollection' does not contain a definition for 'ToArray' and no accessible extension method 'ToArray' accepting a first argument of type 'Dictionary<string, object>.KeyCollection' could be found (are you missing a using directive or an assembly reference?) (CS1061)

This has worked in a previous project and Visual Studio recognises this method but the code doesn't compile.

Any ideas?

1
do you have using System.Linq; in your using statements?Jason
Yeah, here are my usings: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Firebase.Auth; using Firebase.CloudFirestore; using Foundation;Liam Kenny

1 Answers

1
votes

If you want to convert from Dictionary to NSDictionary, you could try the code below. It works well for me. I tested on ios 15.4 with xcode 13.3.

 var document = new Dictionary<string, string>
            {
                {"A", "a"},
                {"B", "b"},
                {"C", "c"},
                {"D", "d"},
                {"E", "e"}
            };

        var iosDoc = NSDictionary.FromObjectsAndKeys(document.Values.ToArray(), document.Keys.ToArray());