Reputation: 773
I'm working to create bindings for Obj-C library so that I can work together with another team together on same project. To ensure this works, I'm attempting to create a simple obj-c library, define bindings, and use that object within MonoTouch. I have no build issue or even runtime exceptions, but my return value from calling any function is always null.
I started by created a new static library using Xcode and defined a 'Calculator' class. Below is the .h and .m
@interface Calculator : NSObject
- (NSString*)getMessage;
@end
@implementation Calculator
- (NSString*)getMessage
{
return @"hey, look at me!";
}
@end
This built into the file 'libXcodeLib.a'. I defined the following MonoTouch bindings for this class and methods:
[BaseType(typeof(NSObject))]
interface Calculator
{
[MonoTouch.Foundation.Export("getMessage")]
string GetMessage();
}
As well as an additional AssemblyInfo.cs for gcc_flags with the following:
[assembly: LinkWith ("libXcodeLib.a", LinkTarget.Simulator | LinkTarget.ArmV6 | LinkTarget.ArmV7, ForceLoad = true)]
And finally, here's a look at the Makefile used to produce the .dll file
BTOUCH=/Developer/MonoTouch/usr/bin/btouch
all: XcodeLib.dll
XcodeLib.dll: Makefile ApiDefinition.cs AssemblyInfo.cs libXcodeLib.a
$(BTOUCH) -e ApiDefinition.cs AssemblyInfo.cs --out=XcodeLib.dll --link-with=libXcodeLib.a,libXcodeLib.a
And here is sample code invoking this method:
var calc = new MTBindings.Calculator();
var msg = calc.GetMessage();
At this point, why is msg NULL?
From all the reading I've done, I cannot see what I'm missing to make this work. Any newer or proven samples/tutorials would be greatly appreciated, I'm not sure how to simplify this more - and yet it still doesn't work!
Thanks to the SO community - I hope :)
Upvotes: 2
Views: 967
Reputation: 32694
I reviewed the package from Matt, and what I found was that his library was an ARM-only library. When run in the simulator, the code would not be linked in, and Objective-C likes to silently continue execution when messages are sent to null objects.
This is what allows the following code in Objective-C to not crash:
id foo = null; [foo.bar.baz saySomething:@"Hello"]
Despite foo being null. In this case, it came back to basically not say anything when the code failed to load the Calculator class. All the messages were being happily sent, but the Objective-C runtime just kept on going.
Upvotes: 2