Objective-C 中的retain 是如何工作的?

发布于 2024-11-03 02:37:54 字数 1605 浏览 1 评论 0原文

我有这个代码。

-(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 技术交流群。

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

发布评论

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

评论(2

西瓜 2024-11-10 02:37:55

currentValuedidStartElement: 完成时被释放。我假设 currentValue 是一个类变量?将 currentValue 分配给 urlToServerLog 后,您会看到什么值?

currentValue is being released when didStartElement: finishes. I assume currentValue is a class variable? What value do you see for urlToServerLog after you assign currentValue to it?

明月夜 2024-11-10 02:37:54

在 -parser:didStartElement:namespaceURI:qualifiedName:attributes: 中,您分配一个新字符串并自动释放它:

currentValue = [[[NSMutableString alloc] init] autorelease];

这意味着当相应的自动释放池耗尽时,该字符串将被释放。一般来说,如果一个对象需要在方法执行完毕后保持活动状态,那么您需要拥有它。在您的情况下,此时不要自动释放它(并记住当您不再需要它时释放它),或者将 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:

currentValue = [[[NSMutableString alloc] init] autorelease];

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 a retain property and use self.currentValue = … instead.

Another point to consider: you probably want to make urlToServerLog a copy property instead of a retain one because currentValue, being a mutable string, will be changed while the XML parser is being executed. If you’re using retain, your urlToServerLog property will also change since it points to the same mutable string. By changing the property to copy, you effectively make a copy of currentValue — any further changes to currentValue won’t affect that property.

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