Objective-C 中的retain 是如何工作的?
我有这个代码。
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"stat"]) {
currentKey = [attributeDict objectForKey:@"name"];
currentValue = [[[NSMutableString alloc] init] autorelease];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
[currentValue appendFormat:string];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([currentKey isEqualToString:@"url"])
self.urlToServerLog = currentValue;
}
头文件
@interface HDEAppDelegate : NSObject <NSApplicationDelegate, NSXMLParserDelegate> {
@private
NSWindow *window;
NSString* scratchFolder;
NSMutableString *urlToServerLog;
NSString *currentKey;
NSMutableString *currentValue;
}
@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSTextField *output;
@property (assign) IBOutlet NSTextField *matchID;
@property (retain) NSString *scratchFolder;
@property (retain) NSMutableString *urlToServerLog;
-(IBAction) ParserButton:(id)sender;
-(NSFileWrapper*)unzip:(NSData*)zipData;
@end
这里的问题是 urlToServerLog/currentValue 处的对象被删除。
我发现一个解决方案是复制对象,但我原来的解决方案仍然有效。 在这种情况下为什么不保留工作?
顺便说一句:scratchFolder 也与 autorelease 对象一起使用,并且工作完美。(以完全相同的方式使用,从 NSString(autorelease) 返回并分配给 scrapFolder。
I have this code.
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"stat"]) {
currentKey = [attributeDict objectForKey:@"name"];
currentValue = [[[NSMutableString alloc] init] autorelease];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
[currentValue appendFormat:string];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([currentKey isEqualToString:@"url"])
self.urlToServerLog = currentValue;
}
And the header file
@interface HDEAppDelegate : NSObject <NSApplicationDelegate, NSXMLParserDelegate> {
@private
NSWindow *window;
NSString* scratchFolder;
NSMutableString *urlToServerLog;
NSString *currentKey;
NSMutableString *currentValue;
}
@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSTextField *output;
@property (assign) IBOutlet NSTextField *matchID;
@property (retain) NSString *scratchFolder;
@property (retain) NSMutableString *urlToServerLog;
-(IBAction) ParserButton:(id)sender;
-(NSFileWrapper*)unzip:(NSData*)zipData;
@end
The problem here is the object at urlToServerLog/currentValue gets removed.
I found a solution is to copy the object but my original solution still stands.
Why doesn't retain work in this case?
BTW: scratchFolder is also used with a autorelease object and it's working perfectly.(Using in the exact same way, return from NSString(autorelease) and assign to scratchFolder.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
currentValue
在didStartElement:
完成时被释放。我假设 currentValue 是一个类变量?将currentValue
分配给urlToServerLog
后,您会看到什么值?currentValue
is being released whendidStartElement:
finishes. I assumecurrentValue
is a class variable? What value do you see forurlToServerLog
after you assigncurrentValue
to it?在 -parser:didStartElement:namespaceURI:qualifiedName:attributes: 中,您分配一个新字符串并自动释放它:
这意味着当相应的自动释放池耗尽时,该字符串将被释放。一般来说,如果一个对象需要在方法执行完毕后保持活动状态,那么您需要拥有它。在您的情况下,此时不要自动释放它(并记住当您不再需要它时释放它),或者将
currentValue
设为retain
属性并使用self.currentValue = ...
相反。需要考虑的另一点:您可能希望将
urlToServerLog
设为copy
属性,而不是retain
属性,因为currentValue
,作为一个可变字符串,将在执行 XML 解析器时更改。如果您使用retain
,您的urlToServerLog
属性也会发生变化,因为它指向相同的可变字符串。通过将属性更改为copy
,您可以有效地复制currentValue
- 对currentValue
的任何进一步更改都不会影响该属性。In
-parser:didStartElement:namespaceURI:qualifiedName:attributes:
, you allocate a new string and autorelease it:This means that the this string will be released when the corresponding autorelease pool is drained. In general, if an object needs to be kept alive after a method has finished executing, you need to own it. In your case, do not autorelease it at this point (and remember to release it when you don’t need it any longer), or make
currentValue
aretain
property and useself.currentValue = …
instead.Another point to consider: you probably want to make
urlToServerLog
acopy
property instead of aretain
one becausecurrentValue
, being a mutable string, will be changed while the XML parser is being executed. If you’re usingretain
, yoururlToServerLog
property will also change since it points to the same mutable string. By changing the property tocopy
, you effectively make a copy ofcurrentValue
— any further changes tocurrentValue
won’t affect that property.