Kevin McMahon

Chicago based mobile developer.

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</span><span style="color: #000000;">.</span><span style="color: #000000;">BBox' does </span><span style="color: #0000FF;">not</span><span style="color: #000000;"> contain a definition </span><span style="color: #0000FF;">for</span><span style="color: #000000;">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</span><span style="color: #000000;">.</span><span style="color: #000000;">BBox' does </span><span style="color: #0000FF;">not</span><span style="color: #000000;"> contain a definition </span><span style="color: #0000FF;">for</span><span style="color: #000000;">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 forMessaging' 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.

Tagged: documentation / gotcha / objective-c / api / binding / monotouch

blog comments powered by Disqus