如何在Objective-C中声明一个字符串?

时间:2021-04-10 19:20:45

How do I declare a simple string "test" to a variable?

如何向变量声明一个简单的字符串“test”?

3 个解决方案

#1


33  

NSString *testString = @"test";

#2


45  

A C string is just like in C.

C字符串就像在C中一样。

char myCString[] = "test";

An NSString uses the @ character:

NSString使用@字符:

NSString *myNSString = @"test";

If you need to manage the NSString's memory:

如果您需要管理NSString的内存:

NSString *myNSString = [NSString stringWithFormat:@"test"];
NSString *myRetainedNSString = [[NSString alloc] initWithFormat:@"test"];

Or if you need an editable string:

或者,如果您需要可编辑的字符串:

NSMutableString *myMutableString = [NSMutableString stringWithFormat:@"test"];

You can read more from the Apple NSString documentation.

您可以从Apple NSString文档中了解更多信息。

#3


9  

Standard string assignment can be done like so:

标准字符串赋值可以这样完成:

NSString *myTestString = @"abc123";

In addition to the basic allocation there are a whole lot of methods you get when using the NSString Class that you don't get with the Standard Char[] array. That is why Objective programming is better!

除了基本分配之外,在使用标准字符[]数组没有的NSString类时,还有很多方法。这就是目标编程更好的原因!

For instance filling a string with the contents of a html webpage, with a single line of code!**

例如,使用单行代码填充带有html网页内容的字符串!**

Creating and Initializing Strings

创建和初始化字符串

+ string
– init
– initWithBytes:length:encoding:
– initWithBytesNoCopy:length:encoding:freeWhenDone:
– initWithCharacters:length:
– initWithCharactersNoCopy:length:freeWhenDone:
– initWithString:
– initWithCString:encoding:
– initWithUTF8String:
– initWithFormat:
– initWithFormat:arguments:
– initWithFormat:locale:
– initWithFormat:locale:arguments:
– initWithData:encoding:
+ stringWithFormat:
+ localizedStringWithFormat:
+ stringWithCharacters:length:
+ stringWithString:
+ stringWithCString:encoding:
+ stringWithUTF8String:

Creating and Initializing a String from a File

从文件创建和初始化字符串

+ stringWithContentsOfFile:encoding:error:
– initWithContentsOfFile:encoding:error:
+ stringWithContentsOfFile:usedEncoding:error:
– initWithContentsOfFile:usedEncoding:error:

Creating and Initializing a String from an URL

从URL创建和初始化字符串

+ stringWithContentsOfURL:encoding:error:
– initWithContentsOfURL:encoding:error:
+ stringWithContentsOfURL:usedEncoding:error:
– initWithContentsOfURL:usedEncoding:error:

If you need a string where you can edit its buffer you want to look at:

如果您需要一个字符串,您可以在其中编辑它想要查看的缓冲区:

NSMutableString

#1


33  

NSString *testString = @"test";

#2


45  

A C string is just like in C.

C字符串就像在C中一样。

char myCString[] = "test";

An NSString uses the @ character:

NSString使用@字符:

NSString *myNSString = @"test";

If you need to manage the NSString's memory:

如果您需要管理NSString的内存:

NSString *myNSString = [NSString stringWithFormat:@"test"];
NSString *myRetainedNSString = [[NSString alloc] initWithFormat:@"test"];

Or if you need an editable string:

或者,如果您需要可编辑的字符串:

NSMutableString *myMutableString = [NSMutableString stringWithFormat:@"test"];

You can read more from the Apple NSString documentation.

您可以从Apple NSString文档中了解更多信息。

#3


9  

Standard string assignment can be done like so:

标准字符串赋值可以这样完成:

NSString *myTestString = @"abc123";

In addition to the basic allocation there are a whole lot of methods you get when using the NSString Class that you don't get with the Standard Char[] array. That is why Objective programming is better!

除了基本分配之外,在使用标准字符[]数组没有的NSString类时,还有很多方法。这就是目标编程更好的原因!

For instance filling a string with the contents of a html webpage, with a single line of code!**

例如,使用单行代码填充带有html网页内容的字符串!**

Creating and Initializing Strings

创建和初始化字符串

+ string
– init
– initWithBytes:length:encoding:
– initWithBytesNoCopy:length:encoding:freeWhenDone:
– initWithCharacters:length:
– initWithCharactersNoCopy:length:freeWhenDone:
– initWithString:
– initWithCString:encoding:
– initWithUTF8String:
– initWithFormat:
– initWithFormat:arguments:
– initWithFormat:locale:
– initWithFormat:locale:arguments:
– initWithData:encoding:
+ stringWithFormat:
+ localizedStringWithFormat:
+ stringWithCharacters:length:
+ stringWithString:
+ stringWithCString:encoding:
+ stringWithUTF8String:

Creating and Initializing a String from a File

从文件创建和初始化字符串

+ stringWithContentsOfFile:encoding:error:
– initWithContentsOfFile:encoding:error:
+ stringWithContentsOfFile:usedEncoding:error:
– initWithContentsOfFile:usedEncoding:error:

Creating and Initializing a String from an URL

从URL创建和初始化字符串

+ stringWithContentsOfURL:encoding:error:
– initWithContentsOfURL:encoding:error:
+ stringWithContentsOfURL:usedEncoding:error:
– initWithContentsOfURL:usedEncoding:error:

If you need a string where you can edit its buffer you want to look at:

如果您需要一个字符串,您可以在其中编辑它想要查看的缓冲区:

NSMutableString