如何将std :: string转换为NSString? [英] How to convert std::string to NSString?

查看:193
本文介绍了如何将std :: string转换为NSString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将标准std::string转换为NSString,但是运气不高.

Hi I am trying to convert a standard std::string into an NSString but I'm not having much luck.

我可以使用以下代码成功地将NSString转换为std::string

I can convert successfully from an NSString to a std::string with the following code

NSString *realm = @"Hollywood";
std::string REALM = [realm cStringUsingEncoding:[NSString defaultCStringEncoding]];

但是当我尝试以下操作时出现编译时错误

However I get a compile time error when I try the following

NSString *errorMessage = [NSString stringWithCString:REALM encoding:[NSString defaultCStringEncoding]];

我得到的错误是

Cannot convert 'std::string' to 'const char*' in argument passing

我在这里想念东西吗?

提前谢谢.

推荐答案

从std :: string中获取c字符串以进行转换:

Get c-string out of std::string for conversion:

NSString *errorMessage = [NSString stringWithCString:REALM.c_str() 
                                   encoding:[NSString defaultCStringEncoding]];

这篇关于如何将std :: string转换为NSString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆