在 MonoMac 中使用数组的正确方法

发布于 2024-10-01 10:33:03 字数 594 浏览 2 评论 0原文

我刚刚开始在 MonoMac 中开发一个项目,到目前为止还很酷。但还有一些事情我不确定。例如:你如何使用数组?这是我发现的:当我从我正在调用的方法返回一个 NSArray 并尝试获取该数组中的自定义对象之一时,我不断收到类似“无法转换”的信息将 System.IntPtr 键入 MyType”。

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

那是我拿回来的数组。但是如果我想自己创建一个数组怎么办? NSArray 的实现不允许我实例化它。因此,如果我的 MonoMac 网站 正确,我应该使用像这样的普通数组,

int[] intArray = int[10];

分别是一个强类型数组,我'我不知道如何在 C# 中使用它。

那么到这里有什么方法呢?

谢谢
–f

I'm just started working on a project in MonoMac, which is pretty cool so far. But there're still some things I'm not sure of. For example: How do you use arrays? Here's what I found out: When I get an NSArray back from a method I'm calling and I try to get one of the custom objects in that array I keep getting something like "cannot convert type System.IntPtr to MyType".

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

That's for arrays I get back. But what if I want to create an array on my own? The implementation of NSArray does not allow me to instantiate it. So if I got the MonoMac website right, I should use an ordinary array like this

int[] intArray = int[10];

respectively an strongly-typed array which I'm not aware of how to use it in C#.

So what's the way to go here?

Thanks
–f

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬シ愛 2024-10-08 10:33:03

一般来说,使用 NSArray 不是很有用,因为您最终会遇到上面描述的问题。

这就是为什么通常应该将 NSArray 转换为强类型数组的原因。 MonoMac 低级运行时已经代表您对所有回调执行此操作。

通常你会这样做:

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

请注意,NSArray只能存储NSObject,因此“YourType”需要是从NSObject派生的对象。

现在,如果您仍然想使用 NSArray,您需要记住的是 ValueAt 返回底层对象句柄(IntPtr),要在 C# 中使用它,您需要将其转换为 NSObject。您使用 Runtime.GetNSObject 方法执行此操作,可以将结果转换为最派生的类型:

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

也就是说,如果您使用 API 绑定工具访问 Objective-C API,则您没有正确绑定事物。你的合约 API 不应该有 NSArray,而应该有强类型版本,所以:

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

应该变成:

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

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

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

Usually you would do this:

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

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

Now, if you still want to use the NSArray, what you need to remember is that the ValueAt returns the underlying object handle (the IntPtr), to use this with C# you need to convert this into an NSObject. You do this with the Runtime.GetNSObject method, you can cast the result to the most derived type:

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

That being said, if you are using the API binding tools to access an Objective-C API, you are not binding things correctly. Your contract API should instead of having an NSArray should have the strongly typed version, so:

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

Should become:

 [Export ("getElements")]
 YourType [] GetElements ();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文