微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift:将 const char ** 输出参数转换为 String

如何解决Swift:将 const char ** 输出参数转换为 String

我正在与使用 const char ** 作为输出参数的 C++ 库(使用 C 中的头文件)进行交互。

执行那个库中的一个方法后,我需要的值写在那个变量中,例如:

CustomMethod(const char **output)

CustomMethod(&output)

// Using the `output` here

通常,在 Swift 中,可以只传递一个标准的 Swift String 作为参数,它会被透明地转换为 const char * (Interacting with C Pointers - Swift Blog)。

例如,我已经在同一个库中多次使用以下构造:

// C
Basicmethod(const char *input)

// Swift
let string = "test"
Basicmethod(string)

但是,当涉及到使用 const char ** 时,我不能像预期的那样将指针传递给 Swift String

// C
CustomMethod(const char **output)

// Swift
var output: String?
CustomMethod(&output)

出现错误

无法将“UnsafeMutablePointer”类型的值转换为 预期的参数类型 'UnsafeMutablePointer' (又名'UnsafeMutablePointer>')

我可以让它工作的唯一方法是直接操作指针:

// C
CustomMethod(const char **output)

// Swift
var output: UnsafePointer<CChar>?
CustomMethod(&output)
let stringValue = String(cString: json)

有什么办法可以使用自动 Swift 字符串到 const char ** 的转换,还是只适用于 const char *

解决方法

桥接的 C 函数需要一个指向 results.[1].name 指针的可变指针,因此您需要提供一个,这里没有自动桥接。

[...]
            try
            {
                timer1.Start();
                if (SearchBox.Text.Contains(SearchBox.Text.ToString()))
                {
                    if (webView21 != null && webView21.CoreWebView2 != null)
                    {
                        Ping pingSender = new Ping();
                        PingOptions options = new PingOptions();

                        // Use the default Ttl value which is 128,// but change the fragmentation behavior.
                        options.DontFragment = true;

                        // Create a buffer of 32 bytes of data to be transmitted.
                        string data = "aaa";
                        byte[] buffer = Encoding.ASCII.GetBytes(data);
                        int timeout = 0;
                        PingReply reply = pingSender.Send(SearchBox.Text.ToString(),timeout,buffer,options);
                        if (reply.Status == IPStatus.Success)
                        {
                            if (SearchBox.Text.StartsWith("https://"))
                            {
                                webView21.CoreWebView2.Navigate(SearchBox.Text);
                            }
                            else if (SearchBox.Text.StartsWith("http://"))
                            {
                                webView21.CoreWebView2.Navigate(SearchBox.Text);
                            }
                            else
                            {
                                webView21.CoreWebView2.Navigate("https://" + SearchBox.Text);
                            }


                        }
                        else
                        {
                            webView21.CoreWebView2.Navigate("https://duckduckgo.com/?q=" + SearchBox.Text + "&t=h_&ia=web");
                        }


                    }
                }
                else if (SearchBox.Text.StartsWith("https://"))
                {
                    if (webView21 != null && webView21.CoreWebView2 != null)
                    {
                        webView21.CoreWebView2.Navigate(SearchBox.Text);

                    }
                }
                else if (SearchBox.Text.StartsWith("http://"))
                {
                    if (webView21 != null && webView21.CoreWebView2 != null)
                    {
                        webView21.CoreWebView2.Navigate(SearchBox.Text);

                    }
                }
                else
                {
                    if (webView21 != null && webView21.CoreWebView2 != null)
                    {
                        webView21.CoreWebView2.Navigate(SearchBox.Text);

                    }
                }
                
                
            }
            catch
            {
                timer1.Start();
                webView21.CoreWebView2.Navigate("https://duckduckgo.com/?q=" + SearchBox.Text + "&t=h_&ia=web");

            }
        }

相同的代码,但采用更 FP 的方式:

CChar

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。