Krumelur
Krumelur

Reputation: 33058

How to convert NSDictionary() with values [PSPDFAnnotationParser class] / [MyCustomAnnotationParser class] into MonoTouch?

From the documentation of a bound API I'm using:

overrideClassNames

Use this to use specific subclasses instead of the default PSPDF* classes. e.g. add an entry of [PSPDFAnnotationParser class] / [MyCustomAnnotationParser class] as key/value pair to use the custom subclass. (MyCustomAnnotationParser must be a subclass of PSPDFAnnotationParser) Throws an exception if the overriding class is not a subclass of the overridden class. Note: does not get serialized when saved to disk.

@property (nonatomic, strong) NSDictionary *overrideClassNames

Here's what I tried but doesn't work. Appearently not strings are required but actual types or something. How can I use this in MonoTouch?

var oClassDic = new NSMutableDictionary();
oClassDic.Add(new NSString("[PSPDFAnnotationParser class]"), new NSString("[PSPDFKitAnnotationParser class]"));
oDoc.OverrideClassNames = oClassDic;

The PSPDFKitAnnotationParser I created like this:

[Register("PSPDFKitAnnotationParser")]
    public class PSPDFKitAnnotationParser : PSPDFAnnotationParser
    {
        public PSPDFKitAnnotationParser () : base()
        {
        }

        public PSPDFKitAnnotationParser (PSPDFDocumentProvider provider) : base(provider)
        {
        }

        public PSPDFKitAnnotationParser (IntPtr handle) : base(handle)
        {
        }

        public PSPDFKitAnnotationParser (NSObjectFlag t) : base(t)
        {
        }

        public PSPDFKitAnnotationParser (NSCoder coder) : base(coder)
        {
        }

Upvotes: 2

Views: 632

Answers (2)

dalexsoto
dalexsoto

Reputation: 3412

Following @poupou answer this might work, I have not tested it this is what you have to do, It Works (TM)

var oClassDic = new NSMutableDictionary();
var key = new Class("PSPDFAnnotationParser");
var val = new Class("PSPDFKitAnnotationParser");

IntPtr selSetObjectForKey = Selector.GetHandle ("setObject:forKey:");

Messaging.void_objc_msgSend_IntPtr_IntPtr (oClassDic.Handle, selSetObjectForKey, val.Handle, key.Handle);

oDoc.OverrideClassNames = oClassDic;

Selector setObject: forKey: expects an ObjC id type on both params

"id" its just a special type that can hold a pointer to any object you can construct with ObjC

So this should work :)

Hope this helps

Alex

Upvotes: 1

poupou
poupou

Reputation: 43553

MonoTouch's own bindings generally hides the class and replace them with, the more .NETy, System.Type.

However the MonoTouch.ObjCRuntime.Class type exists and can also be used. If the native code expects class instances then you should be able to do something like:

var oClassDic = new NSMutableDictionary();
oClassDic.Add(new Class("PSPDFAnnotationParser"), new Class("PSPDFKitAnnotationParser"));
oDoc.OverrideClassNames = oClassDic;

You might have to tweak this a bit since a Class instance is not an NSObject, it's a NativeObject in MonoTouch, so you might have to go one level deeper and use the Handle properties (IntPtr) when adding values/keys to your dictionary.

Upvotes: 2

Related Questions