Monotouch:将对象转换为 NSObject
如何将一个 Object 实例转换为 NSObject 实例?
我已经从这个方法创建了一个 NSDictionary
NSDictionary.FromObjectAndKey();
需要一个 NSObject 但我有自定义对象要传入:
int key = 2341;
var val = new MyClass();
NSDictionary.FromObjectAndKey(val, key); // obviously it does not work!!
如何解决这个问题?先感谢您。
How is it possible to convert an Object instance to NSObject one?
I've created a NSDictionary from
NSDictionary.FromObjectAndKey();
This method wants an NSObject but I have custom object to pass in:
int key = 2341;
var val = new MyClass();
NSDictionary.FromObjectAndKey(val, key); // obviously it does not work!!
How to fix this? Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法将任意对象转换为 NSObject。 NSObject.FromObject 将尝试将常见数据类型(如数字、字符串、矩形、点、转换和一些其他 .NET 类型)包装到其等效的 NSObject 类型中。
在您的特定示例中,“MyClass”必须从 NSObject 派生,然后才能在 NSDictionary 中使用它。
You can not convert an arbitrary object into an NSObject. The NSObject.FromObject will try to wrap common data types like numbers, strings, rectangles, points, transforms, and a handful of other .NET types into their equivalent NSObject types.
In your particular example, "MyClass" would have to derive from an NSObject before you could use it in the NSDictionary.
我能找到的最简单的解决方案是将 .Net 对象包装在 NSObject 中,然后根据需要展开。
使用示例:
The easiest solution I could find was to wrap the .Net object in an NSObject, then unwrap as needed.
Example use:
方法是这样的:
This is the way: