NSString 发布

发布于 2024-12-23 16:27:32 字数 588 浏览 5 评论 0原文

我有这段字符串代码,在尝试释放内存时遇到问题,我知道只有那些释放它的人才会初始化,而不是自动释放,但我在字符串“end”和 nSum 释放方面遇到了问题。

NSString *urlBase = [[NSString alloc] initWithFormat:@"http://service.svc/"];
NSString *op = [[NSString alloc] initWithFormat:@"op1"];
NSString * final = [urlBase stringByAppendingFormat:op];
NSString * nSum = sumTextfield.text;
final = [final stringByAppendingFormat:nSum];

//release

[ urlBase release ];
[ op release ];
//[final release]; error
//[final autorelease]; error

谢谢你的帮助。

更新:

- (IBAction)mostrarOpciones {
 // code (UP)
}

I have this code of string and I have problems trying to free up memory, I have understood that only those who release it initializes and is not autorelease but I had problems with the string "end", and as nSum release.

NSString *urlBase = [[NSString alloc] initWithFormat:@"http://service.svc/"];
NSString *op = [[NSString alloc] initWithFormat:@"op1"];
NSString * final = [urlBase stringByAppendingFormat:op];
NSString * nSum = sumTextfield.text;
final = [final stringByAppendingFormat:nSum];

//release

[ urlBase release ];
[ op release ];
//[final release]; error
//[final autorelease]; error

thank for you help.

UPDATE:

- (IBAction)mostrarOpciones {
 // code (UP)
}

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

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

发布评论

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

评论(2

心欲静而疯不止 2024-12-30 16:27:32

如果您使用以 init、new、copy 或 mutableCopy 开头的方法创建对象,那么您拥有该对象,并负责在使用完该对象后释放它(或自动释放它)。如果您使用任何其他方法创建对象,该对象会自动释放,并且您不需要释放它。在这种情况下,如果您想保留该对象,实际上需要保留该对象。 Apple 有一个内存管理编程指南 包括所有这些规则。

您发布的代码实际上是正确的。您需要释放 urlBaseop,因为您使用以 init 开头的方法创建了它们(在本例中为 initWithFormat:)。 finalnSum 已为您自动发布。 Final 是由不以 init、new、copy 或 mutableCopy 开头的方法创建的(在本例中为工厂方法 stringByAppendingFormat:)。 nSum 由名为 text 的方法返回,您可以假设 sumTextField“拥有”它或在将其返回给您之前已自动释放它,因此您不负责释放它。

If you create an object using a method that begins with init, new, copy, or mutableCopy, then you own that object and are responsible for releasing it (or autoreleasing it) when you're done with it. If you create an object using any other method, that object is autoreleased, and you don't need to release it. In that case, you actually need to retain the object if you want to keep it around. Apple has a Memory Management Programming Guide that includes all these rules.

The code you've posted is actually correct. You need to release urlBase and op because you created them using a method beginning with init (initWithFormat: in this case). final and nSum are already autoreleased for you. final was created by a method that doesn't begin with init, new, copy or mutableCopy (in this case, the factory method stringByAppendingFormat:). nSum was returned by a method called text, and you can assume that sumTextField "owns" it or has autoreleased it before returning it to you, and so you're not responsible for releasing it.

青春有你 2024-12-30 16:27:32

你不能释放你没有分配的NSString。由于您的两个变量没有分配,因此不需要释放它们。

you cannot release NSString which you did not allocate. Since your two variables are not allocated, they need not to be released.

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