我有這段代碼。保留在Objective-C中如何工作?
-(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的對象被刪除。
我發現一個解決方案是複製對象,但我原來的解決方案仍然存在。 爲什麼在這種情況下不保留工作?
BTW:scratchFolder也使用了自動釋放的對象,它的工作完美(使用完全相同的方式,從NSString的(自動釋放回),並分配給scratchFolder
很混亂!!!!! – 2011-04-22 21:33:55