你如何将cstring转换为NSString?

时间:2022-09-15 21:41:40

I have been able to find methods like -[NSString stringWithCString:encoding:] but they do not seem to play well when the cstring is a pointer.

我已经能够找到像[NSString stringWithCString:encoding:]这样的方法,但是当cstring是指针时它们似乎不能很好地发挥作用。

3 个解决方案

#1


First up, don't use initWithCString, it has been deprecated.

首先,不要使用initWithCString,它已被弃用。

Couple of ways you can do this:

有几种方法可以做到这一点:

const *char cString = "Hello";
NSString *myNSString = [NSString stringWithUTF8String:cString];

If you need another encoding like ASCII:

如果您需要其他编码,如ASCII:

const *char cString = "Hello";
NSString *myNSString = [NSString stringWithCString:cString encoding:NSASCIIStringEncoding];

If you want to see all the string encodings available, in Xcode, hold command + option then double click on NSASCIIStringEncoding in the above code block.

如果要查看所有可用的字符串编码,请在Xcode中按住command + option,然后在上面的代码块中双击NSASCIIStringEncoding。

You will be able to see where Apple have declared their enumeration for the string encoding types. Bit quicker than trying to find it in the documentation.

您将能够看到Apple在哪里声明了字符串编码类型的枚举。比试图在文档中找到它快一点。

Some other ones you might need:

您可能需要的其他一些:

NSASCIIStringEncoding
NSUnicodeStringEncoding // same as NSUTF16StringEncoding
NSUTF32StringEncoding

Checkout Apple's NSString Class Reference (encodings are at the bottom of the page)

Checkout Apple的NSString类参考(编码位于页面底部)

#2


With modern Objective-C (since Xcode 5 at least) you can just do:

使用现代Objective-C(至少从Xcode 5开始),你可以做到:

char const* cString = "Hello";
NSString *myNSString = @(cString);

#3


stringWithCString:encoding: creates an NSString from a given C string. To create a C string from an NSString, use either the UTF8String method (generally preferred) or cStringUsingEncoding: (if you need an encoding other than UTF-8).

stringWithCString:encoding:从给定的C字符串创建NSString。要从NSString创建C字符串,请使用UTF8String方法(通常首选)或cStringUsingEncoding :(如果需要UTF-8以外的编码)。

#1


First up, don't use initWithCString, it has been deprecated.

首先,不要使用initWithCString,它已被弃用。

Couple of ways you can do this:

有几种方法可以做到这一点:

const *char cString = "Hello";
NSString *myNSString = [NSString stringWithUTF8String:cString];

If you need another encoding like ASCII:

如果您需要其他编码,如ASCII:

const *char cString = "Hello";
NSString *myNSString = [NSString stringWithCString:cString encoding:NSASCIIStringEncoding];

If you want to see all the string encodings available, in Xcode, hold command + option then double click on NSASCIIStringEncoding in the above code block.

如果要查看所有可用的字符串编码,请在Xcode中按住command + option,然后在上面的代码块中双击NSASCIIStringEncoding。

You will be able to see where Apple have declared their enumeration for the string encoding types. Bit quicker than trying to find it in the documentation.

您将能够看到Apple在哪里声明了字符串编码类型的枚举。比试图在文档中找到它快一点。

Some other ones you might need:

您可能需要的其他一些:

NSASCIIStringEncoding
NSUnicodeStringEncoding // same as NSUTF16StringEncoding
NSUTF32StringEncoding

Checkout Apple's NSString Class Reference (encodings are at the bottom of the page)

Checkout Apple的NSString类参考(编码位于页面底部)

#2


With modern Objective-C (since Xcode 5 at least) you can just do:

使用现代Objective-C(至少从Xcode 5开始),你可以做到:

char const* cString = "Hello";
NSString *myNSString = @(cString);

#3


stringWithCString:encoding: creates an NSString from a given C string. To create a C string from an NSString, use either the UTF8String method (generally preferred) or cStringUsingEncoding: (if you need an encoding other than UTF-8).

stringWithCString:encoding:从给定的C字符串创建NSString。要从NSString创建C字符串,请使用UTF8String方法(通常首选)或cStringUsingEncoding :(如果需要UTF-8以外的编码)。