The right way to use arrays in MonoMac

I just started working on a project at MonoMac, which is still pretty cool. But there is something else that I'm not sure about. For example: how do you use arrays? Here is what I found out: When I return NSArrayback from the method that I call and I try to get one of the user objects in this array, I get something like " cannot convert type System.IntPtr to MyType".

NSArray groupArray = (NSArray)groupDictionary.ObjectForKey(key);
MyType myObject = (MyType)groupArray.ValueAt(0);

What for arrays I come back. But what if I want to create an array myself? The NSArray implementation does not allow me to instantiate. So if I got a MonoMac site , I should use a regular array like this

int[] intArray = int[10];

appropriately strongly typed array, which I do not know about how to use it in C #.

So how to go here?

Thanks
-f

+5
source share
1 answer

In general, using NSArray is not very useful, because you ended up with the problems described above.

This is why, in general, you should convert NSArray to a strongly typed array. MonoMac's low-level runtime does this for all callbacks already on your behalf.

Usually you do this:

YourType [] stronglyTyped = NSArray.ArrayFromHandle<YourType> (arrayIntPtrHandle);

Note that NSArray can only store NSObjects, so "YourType" must be an object derived from NSObject.

, NSArray, , ValueAt (IntPtr), #, NSObject. Runtime.GetNSObject, :

YourType x = (YourType) Runtime.GetNSObject (NSArray.ValueAt (0));

API API Objective-C, . API , NSArray , :

 [Export ("getElements")]
 NSArray GetElements ();

:

 [Export ("getElements")]
 YourType [] GetElements ();
+10

All Articles