xcode NSString 分配和初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9254502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:17:41  来源:igfitidea点击:

NSString allocation and initializing

objective-cxcodecocoa-touchcocoansstring

提问by Jon Wei

What is the difference between:

有什么区别:

NSString *string1 = @"This is string 1.";

and

NSString *string2 = [[NSString alloc]initWithString:@"This is string 2.];

Why am I not allocating and initializing the first string, yet it still works? I thought I was supposed to allocate NSString since it is an object?

为什么我不分配和初始化第一个字符串,但它仍然有效?我以为我应该分配 NSString 因为它是一个对象?

In Cocoa Touch,

在 Cocoa Touch 中,

-(IBAction) clicked: (id)sender{
   NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
   NSString *newLabelText = [[NSString alloc]initWithFormat:@"%@", titleOfButton];
   labelsText.text=newLabelText;
   [newLabelText release];
}

Why do I not allocate and initialize for the titleOfButton string? Does the method I call do that for me?

为什么我不为 titleOfButton 字符串分配和初始化?我调用的方法对我这样做吗?

Also, I'm using XCode 4, but I dislike iOS 5, and such, so I do not use ARC if that matters. Please don't say I should, I am just here to find out why this is so. Thanks!

另外,我使用的是 XCode 4,但我不喜欢 iOS 5 等,所以如果这很重要,我不使用 ARC。请不要说我应该,我只是在这里找出为什么会这样。谢谢!

回答by Alex Reynolds

The variable string1is an NSStringstring literal. The compiler allocates space for it in your executable file. It is loaded into memory and initialized when your program is run. It lives as long as the app runs. You don't need to retainor releaseit.

该变量string1是一个NSString字符串文字。编译器在您的可执行文件中为其分配空间。它会在您的程序运行时加载到内存中并进行初始化。只要应用程序运行,它就会一直存在。你不需要retainrelease它。

The lifespan of variable string2is as long as you dictate, up to the point when you releaseits last reference. You allocate space for it, so you're responsible for cleaning up after it.

变量的生命周期string2与您指定的时间一样长,直到您release最后一次引用为止。你为它分配了空间,所以你有责任在它之后进行清理。

The lifespan of variable titleOfButtonis the life of the method -clicked:. That's because the method -titleForState:returns an autorelease-d NSString. That string will be released automatically, once you leave the scope of the method.

变量titleOfButton的生命周期就是方法的生命周期-clicked:。那是因为该方法-titleForState:返回一个autorelease-d NSString。一旦离开方法的范围,该字符串将自动释放。

You don't need to create newLabelText. That step is redundant and messy. Simply set the labelsText.textproperty to titleOfButton:

您无需创建newLabelText. 这一步是多余的和凌乱的。只需将labelsText.text属性设置为titleOfButton

labelsText.text = titleOfButton;

Why use properties? Because setting this retainproperty will increase the reference count of titleOfButtonby one (that's why it's called a retainproperty), and so the string that is pointed to by titleOfButtonwill live past the end of -clicked:.

为什么要使用属性?因为设置这个retain属性会使 的引用计数增加titleOfButton一(这就是为什么它被称为retain属性),所以指向的字符串titleOfButton将超过-clicked:.

Another way to think about the use of retainin this example is that labelsText.textis "taking ownership" of the string pointed to by titleOfButton. That string will now last as long as labelsTextlives (unless some other variable also takes ownership of the string).

考虑retain在此示例中使用 的另一种方法labelsText.text是“取得”指向的字符串的所有权titleOfButton。该字符串现在将与labelsText生命一样长(除非某些其他变量也拥有该字符串的所有权)。