gpt4 book ai didi

objective-c - iOS - XML 解析内存泄漏

转载 作者:行者123 更新时间:2023-11-29 11:21:33 26 4
gpt4 key购买 nike

帮助 - 我被困在 Objective-C/iOS 炼狱中...

问题:当我将值分配给可以在 View Controller 之间传递值的实例变量时,我得到“额外的 xml 数据”(即 - 幽灵数据)。请参阅下面的屏幕截图。

I think it is a Memory Leak. Your thoughts?

结果的屏幕截图:Console results

代码如下:

-(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 解析相关的实现文件代码的其余部分:

// 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 调用。根据结果​​,我想加载两个 View Controller 之一。如果“总结果”= 0,则加载“NewItemVC”,否则加载“ItemVC”。

最佳答案

首先你不应该使用== @""。更好的方法是:

if ( [totalResults isEqualToString:@"0"]) {
[self.view addSubview:itemNotFound.view];
}

我使用了可用于所有字符串的 isEqualToString:

关于objective-c - iOS - XML 解析内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6918218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com