How to convert NSRect/CGRect back to RectangleF in MonoTouch?

c#, xamarin.ios

Solution

This has been answered here, but just for the record:

My guess is that you're seeing an NSRect wrapped in an NSValue.

Try this:

for(uint i = 0; i < oHighlightAnnot.Rects.Count; ++i)
{
    IntPtr ptrRect = oHighlightAnnot.Rects.ValueAt(i);
    var val = new NSValue(ptrRect);
    var rect = val.RectangleFValue;
}

Problem

(This question if the opposite of what I asked before, when going to way from RectangleF[] to NSArray. See here: How to convert RectangleF[] into NSArray of CGRects?) I have a 3rd party library and bindings for it. One of the bindings is returning an NSArray (note: it is NOT an "`NSArray*`" but a plain "`NSArray`"), therefore returning `RectangleF[]` instead of `NSArray` is not working/supported by MonoTouch. The problem I have now: How to convert the returned `NSArray` (of `NSRect`, that I know) back into an `RectangleF[]`? If I loop through the NSArray: ``` for(uint i = 0; i < oHighlightAnnot.Rects.Count; ++i) { IntPtr ptrRect = oHighlightAnnot.Rects.ValueAt(i); var oObj = new NSObject(ptrRect); } ``` And inspect oObj, I can see in the debugger that it is an NSRect: {NSRect: {{121.3672, 265.76123}, {192.09813, 288}}} But what then? `NSRect` is not exposed, neither is `CGRect` and `RectangleF` is not inheriting from `NSObject`. How to cast/convert back? Heres the method for completion: ``` public virtual NSArray Rects { [Export ("rects")] get { NSArray nSArray; if (this.IsDirectBinding) { nSArray = (NSArray)Runtime.GetNSObject (Messaging.IntPtr_objc_msgSend (base.get_Handle (), PSPDFHighlightAnnotation.selRects)); } else { nSArray = (NSArray)Runtime.GetNSObject (Messaging.IntPtr_objc_msgSendSuper (base.get_SuperHandle (), PSPDFHighlightAnnotation.selRects)); } base.MarkDirty (); this.__mt_Rects_var = nSArray; return nSArray; } [Export ("setRects:")] set { if (this.IsDirectBinding) { Messaging.void_objc_msgSend_IntPtr (base.get_Handle (), PSPDFHighlightAnnotation.selSetRects_, (value != null) ? value.get_Handle () : IntPtr.Zero); } else { Messaging.void_objc_msgSendSuper_IntPtr (base.get_SuperHandle (), PSPDFHighlightAnnotation.selSetRects_, (value != null) ? value.get_Handle () : IntPtr.Zero); } base.MarkDirty (); this.__mt_Rects_var = value; } } ```

Original source