gpt4 book ai didi

mono - 如何绑定(bind)到 iOS Foundations 函数 NSLog

转载 作者:行者123 更新时间:2023-12-04 06:13:51 24 4
gpt4 key购买 nike

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

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

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));
}

关于mono - 如何绑定(bind)到 iOS Foundations 函数 NSLog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7492586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com