1
votes

I have created a static library and trying to bind it to Monotouch using link

In the binding i want to pass NSArray from MonoTouch to Objective C. I have it defined in ApiDefinition.cs like NSObject [] DataArray { get; set; }

  • When i try to pass string array from MonoTouch

var b = new[] { "Item 1", "Item 2", "Item 3" };

get following error

Cannot implicitly convert type 'string[]' to MonoTouch.Foundation.NSObject[]

  • When i try to pass NSArray from MonoTouch

NSArray array1 = NSArray.FromObjects ("Item 1", "Item 2", "Item 3");

get following error

Cannot implicitly convert type MonoTouch.Foundation.NSArray to MonoTouch.Foundation.NSObject[]

How do i pass an Array from MonotOuch to Objective C?

1

1 Answers

5
votes

You have multiple choices when binding an objective-c array, e.g. you could have used NSArray over NSObject[]. I prefer the later but both are valid options.

NSArray array1 = NSArray.FromObjects ("Item 1", "Item 2", "Item 3");

Using an NSArray would make the above code work since it returns an NSArray.

OTOH you have chosen NSObject[] so you must follow your decision, i.e. use an array of NSObject

var b = new[] { "Item 1", "Item 2", "Item 3" };

The above would use an array of System.String. There's no direct conversion between string and NSObject. You can re-write this as:

var b = new NSObject [] { new NSString ("Item 1"), new NSString ("Item 2"), new NSString ("Item 3") };

which should satisfy the compiler and likely, can't be sure without seeing it, is what your native API wants.

Note: if the native API accept an array of NSString then you can bind it at as NSString[].