Binding extern NSString in Monotouch

Go To StackoverFlow.com

2

I am currently working on a MobClix binding and I have come across this line:

extern NSString* const MCAdsErrorDomain;

That line appears outside any class or interface and I am not sure how to bind it in c#. Any help would be greatly appreciated!

2012-04-04 07:07
by Zach


2

Your definition looks C based so it should work just like Rolf's example tell you.

But if/when you're binding an Objective-C library using the btouch tool then you will want to use something like:

    [Field ("XXMyString")]
    NSString MyString { get; }

This is described in details in MonoTouch's binding documentation.

2012-04-04 12:55
by poupou
This is exactly what I had before creating this topic, it doesn't compile because I don't know where to put it. The line of objective-c was outside of all classes and interface declarations. Can I put that field into any class - Zach 2012-04-04 20:35
In general (objective-c wise) the fields are documented with a specific type (where they are used) so that's where we bind them in MonoTouch. However since they are static you could add them anywhere it make sense (including a new type to hold them if you have several of them) - poupou 2012-04-04 20:48
Would it be safest just to include the field in all classes, as I don't actually know where it will be used? Do I have to do anything other then [Field ()] to declare that it is a static field visible to all classes and interfaces - Zach 2012-04-04 21:29
[Field] should be enough. OTOH you can skip it until you need to use it. That way you'll be able to put it where it belongs (if you ever require it). IOW adding unused [Field] does not add any value to your bindings - poupou 2012-04-04 21:35


1

It's done like this:

var handle = Dlfcn.dlopen ("/path/to/mobclix-library", 0);
NSString MCAdsErrorDomain = Dlfcn.GetStringConstant (handle, "MCAdsErrorDomain");
Dlfcn.dlclose (handle);

You can also see a sample here (which includes some error checking too): https://github.com/mono/maccore/blob/master/src/CoreVideo/CVPixelFormatDescription.cs#L67

2012-04-04 11:32
by Rolf Bjarne Kvinge
Ads