Kevin McMahon


Monotouch Binding Gotcha

I stumbled across a subtle gotcha while exploring binding Objective-C types in MonoTouch. As luck would have it, someone else did also and posted a question about it on the MonoTouch IRC channel. The poster of the question eventually came across the answer and shared it there, and I am going to post it here in case anyone else makes the same mistake and is looking for some answers.

I was following along with the documentation for binding new Objective-C types on the MonoTouch site, and as a way to ease into the binding process, I chose a class to define from the CloudMade SDK that I am looking to expose in MonoTouch. The class selected was the BBox class (bbox.h) and I went about creating the following API definition shown below:

using System;using MonoTouch.Foundation;using MonoTouch.ObjCRuntime;namespace CloudMade{    [BaseType(typeof (NSObject))]      interface BBox    {        [Export("westernLongitude")]        float WesternLongitude {get;set;}                [Export("southernLatitude")]        float SouthernLatitude {get;set;}                [Export("easternLongitude")]        float EasternLongitude {get;set;}                [Export("northerLatitude")]        float NorthernLatitude {get;set;}                [Export("asString")]        string AsString();    }}
Once completed, I saved it as BBox.cs and attempted to generate the bindings by invoking btouch on the file.  You can see below the unsuccessful message I received.
$ btouch BBox.cs /var/folders/E4/E44PAZnZGKGpVrmseo2N3++++TI/-Tmp-/9qgrm9nm.lnv/CloudMade/BBox.g.cs(46,71): error CS0117: `CloudMade.BBox' does not contain a definition for `Messaging'/var/folders/E4/E44PAZnZGKGpVrmseo2N3++++TI/-Tmp-/9qgrm9nm.lnv/CloudMade/BBox.g.cs(28,30): (Location of the symbol related to previous error)/var/folders/E4/E44PAZnZGKGpVrmseo2N3++++TI/-Tmp-/9qgrm9nm.lnv/CloudMade/BBox.g.cs(57,71): error CS0117: `CloudMade.BBox' does not contain a definition for `Messaging'/var/folders/E4/E44PAZnZGKGpVrmseo2N3++++TI/-Tmp-/9qgrm9nm.lnv/CloudMade/BBox.g.cs(28,30): (Location of the symbol related to previous error)Compilation failed: 2 error(s), 0 warningsbtouch: API binding contains errors.
After looking at the errors and seeing the**_ `CloudMade.BBox' does not contain a definition for `Messaging'_** message, it looked as if I was missing a using statement.  However, this was not the case as _Messaging_ is a class in the MonoTouch.ObjCRuntime namespace and that namespace was already included in the using statements.  The problem actually turned out to be that the interface had the same name as the file and that had caused the issues shown above as the temporary classes generated during the process caused conflicts.  The solution to this issue was as simple as renaming the file to something other than whatever the interfaces you are defining are named.
So the moral of the story is:

Do not give the file that the API definition is being saved in the same name as one of the interfaces that you are defining.

Steer clear of that and you’ll be binding Objective-C types with ease.