Kevin McMahon

Enthusiast

Blog Search

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, I saw the following message:

'CloudMade.BBox' does not contain a definition for 'Messaging'

It looked as if I was missing a using statement. However, this was not the case. Messaging is a class in the MonoTouch.ObjCRuntime namespace and the using statement declarations already included it. The problem was the interface had the same name as the file. That caused the issue shown above as the temporary classes generated during the process caused conflicts. The solution to this issue was simple. Rename the file something other than what the interface you are defining is named.

So the moral of the story is: Do not give the file that has the API definition the same name as one of the interfaces defined.

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