NSString 分配和初始化
和
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
变量
string1
是一个NSString
字符串文字。编译器在可执行文件中为其分配空间。当程序运行时,它被加载到内存中并初始化。只要应用程序运行,它就会存在。您不需要保留
或释放
它。变量
string2
的生命周期与您指定的一样长,直到您释放
它的最后一个引用为止。您为其分配空间,因此您负责在其之后进行清理。变量
titleOfButton
的生命周期是方法-clicked:
的生命周期。这是因为方法-titleForState:
返回一个autorelease
-dNSString
。一旦离开该方法的范围,该字符串就会自动释放。您不需要创建
newLabelText
。该步骤是多余且混乱的。只需将labelsText.text
属性设置为titleOfButton
:为什么要使用属性?因为设置此
retain
属性将使titleOfButton
的引用计数增加 1(这就是它被称为retain
属性的原因),因此字符串titleOfButton
指向的内容将在-clicked:
结束之后继续存在。在此示例中考虑使用
retain
的另一种方式是,labelsText.text
正在“获取所有权”由titleOfButton
指向的字符串。现在,该字符串将随着labelsText
的存在而持续存在(除非其他某个变量也拥有该字符串的所有权)。The variable
string1
is anNSString
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 toretain
orrelease
it.The lifespan of variable
string2
is as long as you dictate, up to the point when yourelease
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 anautorelease
-dNSString
. 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 thelabelsText.text
property totitleOfButton
:Why use properties? Because setting this
retain
property will increase the reference count oftitleOfButton
by one (that's why it's called aretain
property), and so the string that is pointed to bytitleOfButton
will live past the end of-clicked:
.Another way to think about the use of
retain
in this example is thatlabelsText.text
is "taking ownership" of the string pointed to bytitleOfButton
. That string will now last as long aslabelsText
lives (unless some other variable also takes ownership of the string).