How to create an "NSArray" from a managed (C #) array, for example, `int`s?

I have a managed array ints, call it int[] intArrayand I'm trying to create NSArrayof NSNumberfrom it. What is the easiest way to do this?

+5
source share
2 answers

Given:

int[] intArray = {1,2,3};

You can do:

NSArray nsArray = NSArray.FromObjects(intArray);
+8
source

Your answer is the easiest way if the values ​​of the array (C #) are known when creating the instance NSArray.

An alternative, if you need to change (for example, add more or delete elements), array after creation, create NSMutableArrayand call its method Addto add your own values.

int, NSObject.FromObject , .

int[] intArray = {1,2,3};
var nsArray = new NSMutableArray (3);
foreach (int i in intArray)
   nsArray.Add (NSObject.FromObject (i));
+2

All Articles