Objective-C 初学者(发布)问题

发布于 2024-11-14 07:34:47 字数 637 浏览 2 评论 0原文

我刚刚开始学习 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 技术交流群。

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

发布评论

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

评论(4

ヅ她的身影、若隐若现 2024-11-21 07:34:47

仅释放您声称拥有所有权的对象。这意味着您设置保留或复制的每个属性。不要释放分配的属性。

您可以通过发送 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.

这样的小城市 2024-11-21 07:34:47

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 you alloc 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 use self.name everywhere it is used.

彼岸花似海 2024-11-21 07:34:47

我通过 Google Translate 的 Obj-C to English 过滤器运行了您的代码:

@synthesize name;

亲爱的编译器:请代表我为这个名为 name 的属性创建一个 setter 和一个 getter 方法。

self.name = textInput.text;

获取 textInput.text 中的对象,并使用之前创建的 setter 方法将其放入名为 name 的属性中。

NSString *nameString = name;

创建一个新变量 nameString,并将其指向 name 所指向的同一对象。 (我没有做任何影响该对象的内存管理的事情;我只是想要一个可以用来引用该对象的新标签。)

nameString = @"Cartman";

使我的变量 nameString 指向另一个对象,一个文字字符串。

(这不会改变name指向的对象:
如果名称->对象
nameString = name之后,然后还有nameString ->对象
但如果nameString = OTHER_OBJECT,仍然name -> OBJECT。)

NSString *greeting = [[NSString alloc] 
                  initWithFormat:@"Hello, my name is %@!", nameString];

NSString 对象获取一块正确大小的内存,并使用以下参数设置该内存:....指向变量 greeting 在该对象上。由于我使用 alloc 明确请求了该内存,因此我对此负责。当我不再需要它时,我将释放该对象并释放内存。

label.text = greeting;

获取 greeting 指向的对象,并将其放入 label 名为 text 的属性中。 label 应该对对象执行所需的任何内存管理;那部分不是我的问题。

[greeting release];

我不再需要我创建的那块内存了。赶紧摆脱它吧。

英语比 Objective-C 更冗长一些。 :)

I ran your code through Google Translate's Obj-C to English filter:

@synthesize name;

Dear compiler: please create a setter and a getter method for this property called name on my behalf.

self.name = textInput.text;

Take the object that's in textInput.text and use the previously-created setter method to put it into my property called name.

NSString *nameString = name;

Make a new variable, nameString, and point it at the same object that name 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.)

nameString = @"Cartman";

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 also nameString -> OBJECT,
but if then nameString = OTHER_OBJECT, still name -> OBJECT.)

NSString *greeting = [[NSString alloc] 
                  initWithFormat:@"Hello, my name is %@!", nameString];

Grab a chunk of memory the correct size for an NSString object, and set up that memory with the following arguments:.... Point the variable greeting at that object. Since I explicitly asked for this memory using alloc, I am responsible for it. When I no longer need it, I will release the object and free up the memory.

label.text = greeting;

Take the object that greeting points at, and put it in label's property called text. label should do whatever memory management it needs on the object; that part isn't my problem.

[greeting release];

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. :)

雪花飘飘的天空 2024-11-21 07:34:47

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.

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