Objective-C 初学者(发布)问题
我刚刚开始学习 Objective-C。我读了《Cocoa Been an XCoder》一书,我想我学到了基础知识。现在,我在遵循在线教程时遇到了这段代码:
@synthesize name;
- (IBAction)changeGreeting:(id)sender {
self.name = textInput.text;
NSString *nameString = name;
if([nameString length] == 0) {
nameString = @"Cartman";
}
NSString *greeting = [[NSString alloc]
initWithFormat:@"Hello, my name is %@!", nameString];
label.text = greeting;
[greeting release];
}
我的问题是,我们不应该在 *nameString 变量上调用“release”吗? 或者通过这样做,我也会清理应该在“dealloc”方法中释放的“name”属性? 因为如果我理解正确的话,我必须在函数末尾的函数内部的所有变量上调用“release”,但是在类属性上,我必须仅在“dealloc”方法中调用“release”?
谢谢
I'm just starting to learn Objective-C. I read the Cocoa Become an XCoder book, and I think I learned the basics. Now, I'm following an online tutorial where I encountered this bit of code:
@synthesize name;
- (IBAction)changeGreeting:(id)sender {
self.name = textInput.text;
NSString *nameString = name;
if([nameString length] == 0) {
nameString = @"Cartman";
}
NSString *greeting = [[NSString alloc]
initWithFormat:@"Hello, my name is %@!", nameString];
label.text = greeting;
[greeting release];
}
My question here is, shouldn't we call 'release' also on the *nameString variable?
Or by doing that I would clean also the 'name' property which should be released in the 'dealloc' method?
Cause if I understand correctly, I must call 'release' on all variables located inside functions at the end of those functions, but on the class properties I must call 'release' only in the 'dealloc' method?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
仅释放您声称拥有所有权的对象。这意味着您设置保留或复制的每个属性。不要释放分配的属性。
您可以通过发送 alloc、copy、new 或 mutableCopy 来声明所有权。查看 Apple 的内存管理编程指南 /对象所有权和处置。如果您通过发送保留来手动保留,您也应该释放。
关于这一点,您不必释放 nameString。
Release only the objects you claim ownership on. This means every property you set to retain or copy. Don't release assigned properties.
You claim ownership by sending alloc, copy, new or mutableCopy. Have a look at Apple's Memory Management Programming Guide / Object Ownership and Disposal. You should also release if you retain manually by sending retain.
Regarding this you don't have to release nameString.
greeting
被释放,因为您分配了它。 nameString 不需要释放,因为它是一个赋值。作为一般经验法则,您必须释放分配
的对象。请参阅精彩的 Apple 内存管理指南进一步的帮助。内存管理对于很多iOS初学者来说是一个很大的障碍,内存管理指南基本上应该是必读的。
另外,在这个特定的示例中,您实际上并不需要
nameString
变量,您可以在使用它的任何地方使用self.name
。greeting
is released because you allocated it. nameString doesn't need to be released because it is an assignment. You have to release an object that youalloc
as a general rule of thumb.See the great Apple Memory Management Guide for further help. Memory management is a big hurdle for lots of iOS beginners, and the memory management guide should basically be required reading.
Also, in this specific example, you don't really need the
nameString
variable, you could just useself.name
everywhere it is used.我通过 Google Translate 的 Obj-C to English 过滤器运行了您的代码:
亲爱的编译器:请代表我为这个名为
name
的属性创建一个 setter 和一个 getter 方法。获取
textInput.text
中的对象,并使用之前创建的 setter 方法将其放入名为name
的属性中。创建一个新变量
nameString
,并将其指向name
所指向的同一对象。 (我没有做任何影响该对象的内存管理的事情;我只是想要一个可以用来引用该对象的新标签。)使我的变量
nameString
指向另一个对象,一个文字字符串。(这不会改变
name
指向的对象:如果
名称->对象
,在
nameString = name
之后,然后还有nameString ->对象
,但如果
nameString = OTHER_OBJECT
,仍然name -> OBJECT
。)为
NSString
对象获取一块正确大小的内存,并使用以下参数设置该内存:....指向变量greeting 在该对象上。由于我使用
alloc
明确请求了该内存,因此我对此负责。当我不再需要它时,我将释放
该对象并释放内存。获取
greeting
指向的对象,并将其放入label
名为text
的属性中。label
应该对对象执行所需的任何内存管理;那部分不是我的问题。我不再需要我创建的那块内存了。赶紧摆脱它吧。
英语比 Objective-C 更冗长一些。 :)
I ran your code through Google Translate's Obj-C to English filter:
Dear compiler: please create a setter and a getter method for this property called
name
on my behalf.Take the object that's in
textInput.text
and use the previously-created setter method to put it into my property calledname
.Make a new variable,
nameString
, and point it at the same object thatname
points at. (I'm not doing anything that affects the memory management of that object; I just want a new label that I can use to refer to the object by.)Make my variable
nameString
point at this other object, a literal string.(This does not change the object that
name
points to:If
name -> OBJECT
,after
nameString = name
, then alsonameString -> OBJECT
,but if then
nameString = OTHER_OBJECT
, stillname -> OBJECT
.)Grab a chunk of memory the correct size for an
NSString
object, and set up that memory with the following arguments:.... Point the variablegreeting
at that object. Since I explicitly asked for this memory usingalloc
, I am responsible for it. When I no longer need it, I willrelease
the object and free up the memory.Take the object that
greeting
points at, and put it inlabel
's property calledtext
.label
should do whatever memory management it needs on the object; that part isn't my problem.I have no further need for that chunk of memory that I created. Get rid of it, post haste.
English is quite a bit wordier than Objective-C. :)
nameString 变量只是一个指向 name 类属性中存储的内容的指针。因此,从技术上讲,该字符串存储在 nameString 刚刚指向的内存块中。当您的方法结束时,nameString 指针将从内存中清除。然而,名称仍然会指向该内存。如果释放 nameString,它将清除 name 也指向的内存,因此如果您稍后尝试访问与 name 关联的内存,则会出现问题。
The nameString variable is simply a pointer to whatever is stored in your class property of name. So that string is technically stored in a memory block that nameString just points to. When your method ends, the nameString pointer is simply cleared from memory. However, name will still be pointing at that memory. If you release nameString, it will clear the memory that name also points to and will therefore give you issues later if you try to access the memory associated with name.