关于iPhone:将ObjectiveC类绑定到C#问题

Binding ObjectiveC Class to C# problem

将ObjectiveC类绑定到C#问题

monotouch项目描述了如何绑定用于MonoTouch的Objective-C类型。我们没有为AdMob库执行此操作(另请参见sabonrai点wordpress点com上的monotouch-binding-for-admob博客。

因此,我们决定创建最小的测试项目。我们用两个简单的方法编写了一个简单的objc类,一个返回一个字符串,另一个返回一个整数。

这是TstLib.h:

1
2
3
4
5
6
#import <Cocoa/Cocoa.h>
@interface TstCls : NSObject {
}
- (NSString *) Version;
- (int) GimmeAnInt;
@end

和TstLib.m文件:

1
2
3
4
5
6
7
8
9
#import"TstCls.h"
@implementation TstCls
- (NSString *) Version {
    return @"I ain't got a version, I'm a poor lonesome cowboy...";
}
- (int) GimmeAnInt {
    return 110646;
}
@end

我们有一个小的objc控制台项目来验证该库。这是代码:

1
2
3
4
5
6
7
8
9
#import <Cocoa/Cocoa.h>
#import"../TstLib/TstCls.h"
int main(int argc, char *argv[])
{
    TstCls* tstCls = [[TstCls alloc] init];
    NSLog(@"version = %@", [tstCls Version]);
    NSLog(@"the int = %d", [tstCls GimmeAnInt]);
    return NSApplicationMain(argc,  (const char **) argv);
}

因此,我们为btouch实用程序定义一个绑定文件。

1
2
3
4
5
6
7
8
9
10
using MonoTouch.Foundation;
namespace TstLib {
  [BaseType (typeof (NSObject))]
    interface TstCls {
      [Export ("Version")]
      string Version ();
      [Export ("GimmeAnInt")]
      int GimmeAnInt ();
    }
}

然后我们使用btouch实用程序创建一个libTstLib.a和一个TstLib.dll文件:

1
/Developer/MonoTouch/usr/bin/btouch -o TstLib.dll TstCls.cs

我们现在创建一个新的基于Monotouch窗口的iPhone应用程序" ApiTest",添加包含libTstLib.a和TstLib.dll文件的Lib目录,添加对该TstLib.dll的引用,并将我们的TstLib集成到Main.cs中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using TstCls;
namespace ApiTest
{
  // -gcc_flags"-L${ProjectDir}/Lib -lTstLib -ObjC"
  // or
  // -gcc_flags"-L${ProjectDir}/Lib -lTstLib -force_load ${ProjectDir}/Lib/libTstLib.a"
  public class Application
  {
    static void Main (string[] args)
    {
      UIApplication.Main (args);
    }
  }
  // The name AppDelegate is referenced in the MainWindow.xib file.
  public partial class AppDelegate : UIApplicationDelegate
  {
    // This method is invoked when the application has loaded its UI and its ready to run
    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {
      // If you have defined a view, add it here:
      // window.AddSubview (navigationController.View);

      TstLib.TstCls tstCls = new TstLib.TstCls ();
      Console.WriteLine ("TstLib.TstCls.Version() -> '{0}'", tstCls.Version ());
      Console.WriteLine ("TstLib.TstCls.GimmeAnInt() -> '{0}'", tstCls.GimmeAnInt ());
      window.MakeKeyAndVisible ();
      return true;
    }
    // This method is required in iPhoneOS 3.0
    public override void OnActivated (UIApplication application)
    {
    }
  }
}

这个小项目没有两个Console.Writeline语句。一旦执行Console.WriteLine语句之一,它就会崩溃而没有任何输出。

我们试图尽可能简洁,但仍提供了足够的信息来重新创建测试用例。我们非常愿意提供任何其他信息来帮助解决此问题。

有人看到为什么这不能按预期工作吗?我们将自己限制在最低限度,以测试是否可以为最小的ObjC类提供并使用绑定。

很不幸,它失败了。并且失败的方式与monotouch-binding-for-admob博客中描述的MT_SampleAdMob项目相同。

我们的小项目使用标题为Binding_New_Objective-C_Types的monotouch点网中描述的btouch方法,而MT_SampleAdMob项目使用在相同位置描述的"手动"方法。

这两种方法在类似情况下均会失败。一旦调用了类或实例方法,应用程序就会崩溃而无任何输出。

我们不知道如何解决这个问题并提出解决方案。 Monotouch为许多ObjC类提供c#绑定,因此必须可行。我们已经仔细研究了上面引用的MonoTouch文档。我们看不到MT_SampleAdMob或此btouch方法将在哪里偏离规定的过程,但是都失败了!

实际上,我们非常需要这里的帮助...


您可能没有为本机库禁用THUMB模式。从iOS SDK 3.0开始,苹果链接器在将Thumb库链接到较大的项目时遇到了问题。

您可以通过在Xcode中打开本机库并执行以下操作来禁用拇指模式:

  • 项目->编辑项目设置
  • 在"在构建设置中搜索"中键入" thumb "
  • 取消选框
  • 重建您的本机库。