Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 views
in Technique[技术] by (71.8m points)

xamarin.ios - Monotouch: convert an Object to NSObject

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...