如何绑定iOS基础功能NSLog



我正在尝试创建一个c#绑定到NSLog,以便在我的MonoTouch应用程序中使用。

这里是iOS NSLog (NSLog)文档的链接。到目前为止,我还没有弄清楚确切的语法。下面是我当前的代码:

using System;
using System.Runtime.InteropServices;
namespace CustomTableDemo
{
    public static class FoundationFunctions
    {
        [DllImport("/System/Library/Frameworks/Foundation.framework/Foundation")]
        public extern static void NSLog(string format, params string[] messages);
    }
}

然后按如下方式调用函数:

Console.WriteLine("Before NSLog attempt...");
FoundationFunctions.NSLog("%@", "Hello World");

然后应用程序崩溃,这是我的日志文件。

Before NSLog attempt...
Stacktrace:
  at (wrapper managed-to-native) CustomTableDemo.FoundationFunctions.NSLog (string,string[]) <IL 0x000b1, 0xffffffff>
  at CustomTableDemo.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication,MonoTouch.Foundation.NSDictionary) [0x0000a] in /Users/bartsipes/Projects/CustomTableDemo/CustomTableDemo/Main.cs:31
  at (wrapper runtime-invoke) <Module>.runtime_invoke_bool__this___object_object (object,intptr,intptr,intptr) <IL 0x00066, 0xffffffff>
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff>
  at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00038] in /Developer/MonoTouch/Source/monotouch/monotouch/UIKit/UIApplication.cs:26
  at MonoTouch.UIKit.UIApplication.Main (string[]) [0x00000] in /Developer/MonoTouch/Source/monotouch/monotouch/UIKit/UIApplication.cs:31
  at CustomTableDemo.Application.Main (string[]) [0x00000] in /Users/bartsipes/Projects/CustomTableDemo/CustomTableDemo/Main.cs:14
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>
Native stacktrace:
    0   CustomTableDemo                     0x000c55a3 mono_handle_native_sigsegv + 343
    1   CustomTableDemo                     0x0000f7e8 mono_sigsegv_signal_handler + 322
    2   libSystem.B.dylib                   0x999fb05b _sigtramp + 43
    3   ???                                 0xffffffff 0x0 + 4294967295
    4   CoreFoundation                      0x012fc317 __CFStringAppendFormatCore + 103
    5   CoreFoundation                      0x01245507 _CFStringCreateWithFormatAndArgumentsAux + 119
    6   CoreFoundation                      0x012cd1ee _CFLogvEx + 142
    7   Foundation                          0x0065a9a4 NSLogv + 143
    8   Foundation                          0x0065a913 NSLog + 27
    9   ???                                 0x07fdd694 0x0 + 134076052
    10  ???                                 0x05e26c1a 0x0 + 98724890
    11  ???                                 0x05e26eeb 0x0 + 98725611
    12  CustomTableDemo                     0x0000f5a3 mono_jit_runtime_invoke + 1332
    13  CustomTableDemo                     0x001d9411 mono_runtime_invoke + 137
    14  CustomTableDemo                     0x0027d9d7 monotouch_trampoline + 2527
    15  UIKit                               0x00884c89 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
    16  UIKit                               0x00886d88 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 439
    17  UIKit                               0x00891617 -[UIApplication handleEvent:withNewEvent:] + 1533
    18  UIKit                               0x00889abf -[UIApplication sendEvent:] + 71
    19  UIKit                               0x0088ef2e _UIApplicationHandleEvent + 7576
    20  GraphicsServices                    0x01a94992 PurpleEventCallback + 1550
    21  CoreFoundation                      0x012f5944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    22  CoreFoundation                      0x01255cf7 __CFRunLoopDoSource1 + 215
    23  CoreFoundation                      0x01252f83 __CFRunLoopRun + 979
    24  CoreFoundation                      0x01252840 CFRunLoopRunSpecific + 208
    25  CoreFoundation                      0x01252761 CFRunLoopRunInMode + 97
    26  UIKit                               0x008867d2 -[UIApplication _run] + 623
    27  UIKit                               0x00892c93 UIApplicationMain + 1160
    28  ???                                 0x05e222db 0x0 + 98706139
    29  ???                                 0x05e220ac 0x0 + 98705580
    30  ???                                 0x05e21e7c 0x0 + 98705020
    31  ???                                 0x05e21cd4 0x0 + 98704596
    32  ???                                 0x05e21e26 0x0 + 98704934
    33  CustomTableDemo                     0x0000f5a3 mono_jit_runtime_invoke + 1332
    34  CustomTableDemo                     0x001d9411 mono_runtime_invoke + 137
    35  CustomTableDemo                     0x001dba70 mono_runtime_exec_main + 669
    36  CustomTableDemo                     0x001dae7e mono_runtime_run_main + 843
    37  CustomTableDemo                     0x0009c573 mono_jit_exec + 200
    38  CustomTableDemo                     0x00002df0 main + 4060
    39  CustomTableDemo                     0x00001bf9 _start + 208
    40  CustomTableDemo                     0x00001b28 start + 40
Debug info from gdb:
dyld: could not load inserted library: /Users/bartsipes/Library/Application Support/iPhone Simulator/4.3/Applications/599C50F9-29E2-4402-AA84-7B6DC1A1CD36/CustomTableDemo.app/monotouch-fixes.dylib

=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

它不起作用的原因是p/Invoke将params参数转换为数组,这不是NSLog期望的。NSLog期望在堆栈上有一个变量列表参数,而不是在堆栈上有一个指向数组的单一参数。

简单的解决方案是提供多个重载,如下所示:
[DllImport (...)]
public extern static void NSLog(string format, string arg1);
[DllImport (...)]
public extern static void NSLog(string format, string arg1, string arg2);
[DllImport (...)]
public extern static void NSLog(string format, string arg1, string arg2, string arg3);

如果你不依赖NSLog字符串格式化规则,你只需要第一个版本,然后你可以包装一个方法:

public void MyLog (string format, params object [] args)
{
    NSLog (String.Format (format, args));
}

可以:

[DllImport ("/System/Library/Frameworks/Foundation.framework/Foundation")]
static external void  NSLog(IntPtr format, IntPtr arg1);
static void NSLog(string s)
{
  NSString p1 = "%@";
  NSString p2 = s;
  NSLog(p1.NativePointer, p2.NativePointer);
}

我带了@Ck。

[DllImport ("/System/Library/Frameworks/Foundation.framework/Foundation")]
public extern static void NSLog (IntPtr format, IntPtr arg1);
static void NSLog (string s)
{
    NSString p1 = new NSString ("%@");
    NSString p2 = new NSString (s);
    NSLog (p1.Handle, p2.Handle);
}

我是从Objective-C/Cocoa背景中回答的,没有monotouch经验,但我怀疑你有这个问题,因为你的monotouch字符串不是一个原生的NSString,但那是你告诉NSLog去寻找的。

从一些快速的研究来看,monotouch可能支持在它的字符串和NSString之间隐式转换,但是这些不会在这里被触发,因为NSLog的参数是无类型的。

尝试在调用NSLog之前将"Hello World"转换为NSString,或者尝试将格式参数从%@ (NSObject)更改为%s (C-Style String)。

最新更新