NSString 分配和初始化

发布于 2025-01-05 02:11:12 字数 694 浏览 2 评论 0原文

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

为什么

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

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

在 Cocoa Touch 中,

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

为什么我不分配并初始化 titleOfButton 字符串?我调用的方法可以为我做到这一点吗?

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

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?

In 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?

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜未央樱花落 2025-01-12 02:11:12

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

变量string2的生​​命周期与您指定的一样长,直到您释放它的最后一个引用为止。您为其分配空间,因此您负责在其之后进行清理。

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

您不需要创建newLabelText。该步骤是多余且混乱的。只需将 labelsText.text 属性设置为 titleOfButton

labelsText.text = titleOfButton;

为什么要使用属性?因为设置此 retain 属性将使 titleOfButton 的引用计数增加 1(这就是它被称为 retain 属性的原因),因此字符串titleOfButton 指向的内容将在 -clicked: 结束之后继续存在。

在此示例中考虑使用 retain 的另一种方式是,labelsText.text 正在“获取所有权”由 titleOfButton 指向的字符串。现在,该字符串将随着 labelsText 的存在而持续存在(除非其他某个变量也拥有该字符串的所有权)。

The variable string1 is an NSString string 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 retain or release it.

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

The lifespan of variable titleOfButton is 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.

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

labelsText.text = titleOfButton;

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文