问题
The code I am trying to bind puts a very important key in one of the .h files:
#define xAppKey @"REPLACE_WITH_YOUR_APP_KEY"
This value is then reused throughout the ObjC code. I am suspicious that in the binding process, this value does not carry through. Based on a few other threads on SO, that seems to be the case, at least. Is my problem likely something else, or is there a chance this value is not being honored in the wrapped code?
I'm sorry if my objective-c terminology is off, I know nothing about it.
EDIT
I just wanted to clarify what may not have been evident. By "binding" I am referring to binding it to monotouch as a native library. If that was clear already, carry on...
回答1:
The point of having a #define
in an Obj-C .h
is to allow it to be replaced by the application leveraging the library. In this case, your (Obj-C) library is probably distributed as sources only.
But you want to create (Xamarin.iOS) bindings for it. So you need to compile this library, and compiling will fix the value of the xApiKey
once and for all.
Your aim is probably to expose this xApiKey
to the consuming application through Xamarin.iOS. You'll have to modify the Obj-C
library to use a property instead of a constant, then bind the (static) property accessors through Xamarin.iOS
回答2:
xAppKey is a macro. It will not be bound at all. On the contrary. On pre-compile time it will be replaced with @"REPLACE_WITH_YOUR_APP_KEY"
and then compiled as if this string was repeated all the time.
For exactly this reason some experts suggest replacing them with constant strings. These experts may be right. However, I like the #define.
And for exactly this reason you should not add any comments to the #define statement.
#define xAppKey @"REPLACE_WITH_YOUR_APP_KEY" // This is the application key
Things like that will almost certainly cause compile time errors which may be hard to understand. Example:
NSLog (@"%@", xAppKey);
That works fine withot the comment, because it will be converted to:
NSLog (@"%@", @"REPLACE_WITH_YOUR_APP_KEY");
But with the comment it will be converted to:
NSLog (@"%@", @"REPLACE_WITH_YOUR_APP_KEY" // This is the application key);
And that's not gonna compile.
来源:https://stackoverflow.com/questions/15350740/binding-define-used-as-constant