开发者

iOS - XML Parsing Memory Leak

开发者 https://www.devze.com 2023-03-25 00:02 出处:网络
Help - I\'m stuck in Objective-C/iOS Purgatory... ISSUE:When I assign values to instance variables that can pass values between view controllers I get "extra xml data" (i.e. - ghost data).Pl

Help - I'm stuck in Objective-C/iOS Purgatory...

ISSUE: When I assign values to instance variables that can pass values between view controllers I get "extra xml data" (i.e. - ghost data). Please see screen shot below.

I think it is a Memory Leak. Your thoughts?

Screen shot of results:

iOS - XML Parsing Memory Leak

Here's the code:

-(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开发者_如何学JAVA.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);    

}

Remainder of implementation file code related to 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];
    
}
}

UPDATE: Based on feedback from dragon112, I fixed my "string literal" issue...

FIXED ISSUE: I'm parsing a Google API call. Based on the results, I would like to load one-of-two view controllers. If the "total results" = 0, load "NewItemVC", else load "ItemVC".


First of all you should not use == @"". The better way is:

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

I used the isEqualToString: which can be used on all strings.

0

精彩评论

暂无评论...
验证码 换一张
取 消