有没有用setTitle(UIButton类)自动释放的?
众所周知,setTitle会自动保留作为参数传递的字符串。当需要更改按钮标题时,我想有必要在设置新字符串之前释放当前(旧)字符串。我想知道最优雅的点缀方式是什么。
请参阅我的代码示例(此处,getPlayerHandFromGame 方法生成自动释放的字符串,这些字符串在调用 setTitle 时保留):
colourString = [pGame getPlayerHandFromGame:1 withColour:COLOUR_HEARTS];
// Split colourString into array of strings if not null.
if ([colourString length] != 0) {
listCards = [colourString componentsSeparatedByString:@" "];
for (cardCounterSameColour = 1; cardCounterSameColour <= [listCards count]; cardCounterSameColour ++) {
currentCardButton = [self buttonCardNumber:cardCounter];
// Objects are numbered from 0 in the array
[currentCardButton setTitle:[listCards objectAtIndex:cardCounterSameColour-1] forState:UIControlStateNormal];
cardCounter ++;
}
}
由于按钮标题将更新多次,因此这部分代码将被调用多次。我想在设置标题之前,我应该做这样的事情:
[currentCardButton titleForState:UIControlStateNormal release]
为了释放不再使用的字符串(titleForState返回一个指向NSString的指针)。
这是避免设备内存加载未使用的字符串的正确方法吗?
非常感谢, 苹果92
As everyone knows, setTitle automatically retains the string passed as a parameter. When the button caption needs to be changed, I guess that it is necesary to release the current (old) string before setting the new one. I wonder what is the most elegant way to dot it.
See my code sample (here, getPlayerHandFromGame method produces autoreleased strings which are retained when setTitle is invoked):
colourString = [pGame getPlayerHandFromGame:1 withColour:COLOUR_HEARTS];
// Split colourString into array of strings if not null.
if ([colourString length] != 0) {
listCards = [colourString componentsSeparatedByString:@" "];
for (cardCounterSameColour = 1; cardCounterSameColour <= [listCards count]; cardCounterSameColour ++) {
currentCardButton = [self buttonCardNumber:cardCounter];
// Objects are numbered from 0 in the array
[currentCardButton setTitle:[listCards objectAtIndex:cardCounterSameColour-1] forState:UIControlStateNormal];
cardCounter ++;
}
}
This portion of code will be called several times since the button caption will be updated several times. I guess that before setting the title, I should do something like this:
[currentCardButton titleForState:UIControlStateNormal release]
in order to release the string which will not be used anymore (titleForState returns a pointer to the NSString).
Is that the right way to avoid that the device memory gets loaded with unused strings ?
Many thanks,
Apple92
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
任何保留其属性之一的值集的类还负责在该属性的值再次更改时释放旧值。别担心。
Any class that retains a value set on one of its properties is also responsible for releasing the old value when that property's value gets changed again. Don't worry about it.
真的吗?我不知道。事实上,我敢打赌一品脱啤酒它不会保留字符串,而是复制它。
诚然,对于 NSString,-copy 可能会被实现为保留并返回 self,但如果您将 NSMutableString 传递给它,则会发生真正的副本。
再猜一次,笨蛋!
或者不那么滑稽地说:任何对象都有责任管理它想要保留的其他对象的所有权。一旦你在 setTitle: 中传递了标题,你就不需要担心对象在获得新标题后如何处理它。
考虑代码:
并应用 内存管理规则到返回结果。
你是通过alloc、new还是copy获得的?没有,你保留了吗?否(请记住,我们正在讨论由方法传回的对象,而不是您最初提供给它的对象)。因此,您不得释放它。
Really? I don't know it. In fact, I'll bet a pint of beer that it doesn't retain the string, but copies it.
Granted, for an NSString, -copy is probably implemented as doing a retain and returning self, but if you pass it an NSMutableString, a genuine copy will occur.
Guess again, sucker!
Or less facetiously: any object is responsible for managing the ownership of other objects it wantsa to keep hold of. Once you have passed the title in setTitle: you do not need to worry about how the object disposes of it once it gets a new one.
Consider the code:
and apply the memory management rules to the return result.
Did you obtain it with alloc, new or copy? No. Did you retain it? No (remember we are talking about the object passed back by the method,not the object you originally gave it). Therefore, you must not release it.
看一下对象所有权约定:
每个类都应该遵循这些规则(苹果就是这样做的),所以为了它们你不必担心,因为你还没有在你的类中进行分配/保留,并且按钮将保留它以供内部使用。
Take a look at the object ownership convention:
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html#//apple_ref/doc/uid/20000043-BEHDEDDB
Every class should follow these rules (Apple's does it), so in order to them you dont have to worry because you haven't done a alloc/retain in your class, and the button will retain it for it's internal use.