简体   繁体   English

在Objective-C中使用ARC转换C ++类型

[英]Casting C++ types with ARC in objective-c

Stumbled with the following situation: 遇到以下情况:

I work with a 3-rd party C++ library. 我使用第3方C ++库。

lib's header: lib的标头:

#define lib_fname               char 
typedef lib_fname*              lib_l_fname;

Function(lib_l_fname name);

my code(I need to call that func): 我的代码(我需要调用该函数):

Function ((lib_l_fname)[@"name" UTF8String]);

This worked in not-ARC project, but in with ARC i have an error - "Cast of an Objective-C pointer to lib_l_fname (aka char*) is disallowed with ARC" 这在非ARC项目中有效,但在ARC中出现错误-“ ARC不允许使用指向lib_l_fname(aka char *)的Objective-C指针”

I tried 我试过了

  Function ([@"name" UTF8String]);

But it didn't work. 但这没有用。 Is there a solution? 有解决方案吗?

Found a solution: 找到一个解决方案:

Compiling with arc is more strict and it gives errors when trying to cast const char* to char*. 使用arc进行编译更加严格,并且在将const char *强制转换为char *时会产生错误。

The solution was to create a second string char* and to strcpy there [@"name" UTF8String] 解决的办法是创建第二个字符串char *并在那里strcpy [@“ name” UTF8String]

 char str2[1024] = {0};
 strcpy(str2, [@"name" UTF8String]);

 Function ((lib_l_fname)str2);

works as expected 按预期工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM