【发布时间】:2011-10-18 14:15:20
【问题描述】:
帮助 - 我被困在 Objective-C/iOS Purgatory...
问题:当我将值分配给可以在视图控制器之间传递值的实例变量时,我得到“额外的 xml 数据”(即 - 幽灵数据)。请看下面的屏幕截图。
我认为这是内存泄漏。你的想法?
结果截图:
代码如下:
-(void)parserDidEndDocument:(NSXMLParser *)parser
{
itemNotFound = [[NewItemViewController alloc]
initWithNibName:@"NewItemViewController"
bundle:nil];
itemFound = [[ItemViewController alloc]
initWithNibName:@"ItemViewController"
bundle:nil];
// If # of items from Google API request equals zero, then go to User Generated Item Screen
if ( totalResults == @"0") {
[self.view addSubview:itemNotFound.view];
} else {
[self.view addSubview:itemFound.view];
}
[totalResults release];
totalResults = nil;
// Added 7-13-2011: Assign UPC code to a variable to use in Item View
NSString *upcItem = [[NSString alloc]
initWithString:itemNumber];
NSString *titleItem = [[NSString alloc]
initWithString:itemDesc];
NSString *brandItem = [[NSString alloc]
initWithString:itemBrand];
NSString *storeItem = [[NSString alloc]
initWithString:itemStore];
NSString *imageItem = [[NSString alloc]
initWithString:[itemImagesArray objectAtIndex:0]];
self.itemFound.upcScanned = upcItem;
self.itemFound.titleScanned = titleItem;
self.itemFound.brandScanned = brandItem;
self.itemFound.storeScanned = storeItem;
self.itemFound.imageScanned = imageItem;
NSLog(@"RetainedUPC = %@", self.itemFound.upcScanned);
NSLog(@"RetainedTitle = %@", self.itemFound.titleScanned);
NSLog(@"RetainedBrand = %@", self.itemFound.brandScanned);
NSLog(@"RetainedStore = %@", self.itemFound.storeScanned);
NSLog(@"RetainedImage = %@", self.itemFound.imageScanned);
}
与XML Parsing相关的实现文件代码的其余部分:
// This delegate method will inform the application of new elements and collect the associated strings
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
// totalResults is a counter to determine if the item was found on the Google Search API
if ([elementName isEqual:@"openSearch:totalResults"]) {
NSLog(@"found totalResults!");
if (!totalResults)
totalResults = [[NSMutableString alloc] init];
return;
}
//********** test to ensure Product Element is found ********/
/*
if ([elementName isEqual:@"s:product"]) {
NSLog(@"found Product!");
if (!productScanned)
productScanned = [[NSMutableArray alloc] init];
return;
}*/
//******* EOF: test to ensure Product Element is found ******/
if ([elementName isEqual:@"s:gtin"]) {
// set ItemNumber as the GTIN of the productScanned
itemNumber = [[NSMutableString alloc] init];
}
if ([elementName isEqual:@"s:title"]) {
// set ItemDesc as the item description of the productScanned
itemDesc = [[NSMutableString alloc] init];
}
if ([elementName isEqual:@"s:brand"]) {
// set ItemBrand as the brand description of the productScanned
itemBrand = [[NSMutableString alloc] init];
}
if ([elementName isEqual:@"s:name"]) {
// set ItemStore as the store location of the productScanned
itemStore = [[NSMutableString alloc] init];
}
// Create array of Image Tags
if ([elementName isEqual:@"s:images"]) {
NSLog(@"found Images!");
if (!itemImageURLArray)
itemImageURLArray = [[NSMutableArray alloc] init];
return;
}
if ([elementName isEqual:@"s:image"]) {
// itemImagesArray is an NSMutableArray instance variable
if (!itemImagesArray)
itemImagesArray = [[NSMutableArray alloc] init];
NSString *thisLink = [attributeDict objectForKey:@"link"];
if (thisLink)
// do something
[itemImagesArray addObject:thisLink];
return;
}
}
// This method appends item information to the text objects on the view
- (void)parser:(NSXMLParser *)parser
foundCharacters:(NSString *)string
{
[totalResults appendString:string];
[itemNumber appendString:string];
[itemDesc appendString:string];
[itemBrand appendString:string];
[itemStore appendString:string];
}
// Take the text found in each element and add the final string to itemDetails
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
// totalResults is a counter to determine if the item was found on the Google Search API
if ([elementName isEqual:@"openSearch:totalResults"]) {
NSLog(@"ended totalResults: %@", totalResults);
}
if ([elementName isEqual:@"s:gtin"]) {
NSLog(@"ended gtin: %@", itemNumber);
[itemDetails addObject:itemNumber];
//upcField.text = itemNumber;
}
if ([elementName isEqual:@"s:title"]) {
NSLog(@"ended title: %@", itemDesc);
[itemDetails addObject:itemDesc];
//itemField.text = itemDesc;
}
if ([elementName isEqual:@"s:brand"]) {
NSLog(@"ended brand: %@", itemBrand);
[itemDetails addObject:itemBrand];
//brandField.text = itemBrand;
}
if ([elementName isEqual:@"s:name"]) {
NSLog(@"ended store: %@", itemStore);
[itemDetails addObject:itemStore];
//storeField.text = itemStore;
}
if ([elementName isEqual:@"s:image"]) {
NSLog(@"ended image: %@", itemImagesArray);
[itemDetails addObject:itemImagesArray];
}
}
更新:根据dragon112 的反馈,我修复了我的“字符串文字”问题...
已修复问题:我正在解析 Google API 调用。根据结果,我想加载两个视图控制器之一。如果“总结果”=0,则加载“NewItemVC”,否则加载“ItemVC”。
【问题讨论】:
标签: objective-c ios memory-leaks memory-management nsxmlparser