Error[8]: Undefined offset: 4, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我在 Swift 3上,我需要与一个C API进行交互,它接受一个以NULL结尾的字符串列表,例如 const char *cmd[] = {"name1", "value1", NULL};command(cmd); 在Swift中,API被导入为 func command(_ args: UnsafeMutablePointer<UnsafePointer<Int8>?>!) 在使用类型转换 @H_419_2@ 我在 Swift 3上,我需要与一个C API进行交互,它接受一个以NulL结尾的字符串列表,例如

const char *cmd[] = {"name1","value1",NulL};command(cmd);

在Swift中,API被导入为

func command(_ args: UnsafeMutablePointer<UnsafePointer<Int8>?>!)

在使用类型转换或unsafeAddress(of :)尝试了数百次后,我仍然无法完成这项工作.即使我传递了一个通过编译的有效指针,它也会在运行时崩溃,说无效的内存访问(在strlen函数中).或者也许这是关于ARC的事情?

let array = ["name1",nil]// ???// args: UnsafeMutablePointer<UnsafePointer<Int8>?>command(args)
解决方法 你可以像 How to pass an array of Swift strings to a C function taking a char ** parameter一样进行.由于它的不同,它有点不同
参数数组的常量,因为有一个终止
nil(不能传递给strdup()).

这是它应该如何工作:

let array: [String?] = ["name1","name2",nil]// Create [UnsafePointer<Int8>]:var cargs = array.map { [+++].flatMap { UnsafePointer<Int8>(strdup([+++])) } }// Call C function:let result = command(&cargs)// Free the duplicated strings:for ptr in cargs { free(UnsafeMutablePointer(mutating: ptr)) }
@H_419_2@ 总结

以上是内存溢出为你收集整理的将Swift数组String转换为C字符串数组指针全部内容,希望文章能够帮你解决将Swift数组String转换为C字符串数组指针所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 5, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我在 Swift 3上,我需要与一个C API进行交互,它接受一个以NULL结尾的字符串列表,例如 const char *cmd[] = {"name1", "value1", NULL};command(cmd); 在Swift中,API被导入为 func command(_ args: UnsafeMutablePointer<UnsafePointer<Int8>?>!) 在使用类型转换 @H_419_2@ 我在 Swift 3上,我需要与一个C API进行交互,它接受一个以NulL结尾的字符串列表,例如

const char *cmd[] = {"name1","value1",NulL};command(cmd);

在Swift中,API被导入为

func command(_ args: UnsafeMutablePointer<UnsafePointer<Int8>?>!)

在使用类型转换或unsafeAddress(of :)尝试了数百次后,我仍然无法完成这项工作.即使我传递了一个通过编译的有效指针,它也会在运行时崩溃,说无效的内存访问(在strlen函数中).或者也许这是关于ARC的事情?

let array = ["name1",nil]// ???// args: UnsafeMutablePointer<UnsafePointer<Int8>?>command(args)
解决方法 你可以像 How to pass an array of Swift strings to a C function taking a char ** parameter一样进行.由于它的不同,它有点不同
参数数组的常量,因为有一个终止
nil(不能传递给strdup()).

这是它应该如何工作:

let array: [String?] = ["name1","name2",nil]// Create [UnsafePointer<Int8>]:var cargs = array.map { .flatMap { UnsafePointer<Int8>(strdup([+++])) } }// Call C function:let result = command(&cargs)// Free the duplicated strings:for ptr in cargs { free(UnsafeMutablePointer(mutating: ptr)) }
@H_419_2@ 总结

以上是内存溢出为你收集整理的将Swift数组String转换为C字符串数组指针全部内容,希望文章能够帮你解决将Swift数组String转换为C字符串数组指针所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
将Swift数组String转换为C字符串数组指针_app_内存溢出

将Swift数组String转换为C字符串数组指针

将Swift数组String转换为C字符串数组指针,第1张

概述我在 Swift 3上,我需要与一个C API进行交互,它接受一个以NULL结尾的字符串列表,例如 const char *cmd[] = {"name1", "value1", NULL};command(cmd); 在Swift中,API被导入为 func command(_ args: UnsafeMutablePointer<UnsafePointer<Int8>?>!) 在使用类型转换 @H_419_2@ 我在 Swift 3上,我需要与一个C API进行交互,它接受一个以NulL结尾的字符串列表,例如

const char *cmd[] = {"name1","value1",NulL};command(cmd);

在Swift中,API被导入为

func command(_ args: UnsafeMutablePointer<UnsafePointer<Int8>?>!)

在使用类型转换或unsafeAddress(of :)尝试了数百次后,我仍然无法完成这项工作.即使我传递了一个通过编译的有效指针,它也会在运行时崩溃,说无效的内存访问(在strlen函数中).或者也许这是关于ARC的事情?

let array = ["name1",nil]// ???// args: UnsafeMutablePointer<UnsafePointer<Int8>?>command(args)
解决方法 你可以像 How to pass an array of Swift strings to a C function taking a char ** parameter一样进行.由于它的不同,它有点不同
参数数组的常量,因为有一个终止
nil(不能传递给strdup()).

这是它应该如何工作:

let array: [String?] = ["name1","name2",nil]// Create [UnsafePointer<Int8>]:var cargs = array.map { .flatMap { UnsafePointer<Int8>(strdup()) } }// Call C function:let result = command(&cargs)// Free the duplicated strings:for ptr in cargs { free(UnsafeMutablePointer(mutating: ptr)) }
@H_419_2@ 总结

以上是内存溢出为你收集整理的将Swift数组String转换为C字符串数组指针全部内容,希望文章能够帮你解决将Swift数组String转换为C字符串数组指针所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1007240.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存