Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-21-2011, 08:59 AM   #1 (permalink)
r@t
Registered Member
 
Join Date: Jan 2011
Posts: 21
r@t is on a distinguished road
Default NSXMLParser to NSMutableArray

Hi, I can't seem to get this single array to save into another object's NSMutableArray. I can't even get a count from "mArray". Ideas to a solution would be appreciated. I have looked on previous threads but still can't see what i am doing wrong. Thanks

Code:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [nodeContent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{			
    //NSLog(@"found this element: %@", elementName);
	currentElement = [elementName copy];
    
	if ([elementName isEqualToString:@"z:row"]) {
        
        currentTitle=[[NSMutableString alloc]init];
        currentTitle =[attributeDict valueForKey:@"ows_Title"]; 
        
        NSLog(@"title:%@",currentTitle);
    }
}   

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

	//NSLog(@"ended element: %@", elementName);
	if ([elementName isEqualToString:@"z:row"]) {
        
        [mArray addObject:currentTitle];
              
    }
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
	
	//[activityIndicator stopAnimating];
	//[activityIndicator removeFromSuperview];
    
    NSMutableArray *tempDepartments = [[NSMutableArray alloc] initWithArray:mArray];
    accountData.departments = tempDepartments;
    [tempDepartments release];
	
	NSLog(@"parse complete");
    NSLog(@"accountData.departments: %d", [accountData.departments count]);
    
}


- (void)dealloc {
    
    [convertToStringData release];
    [accountData release];
    [mArray release];
    [super dealloc];
}

@end
the log is as follows:


</listitems></GetListItemsResult></GetListItemsResponse></soap:Body></soap:Envelope>
2011-11-21 15:33:03.309 ProjectTM[28209:f803] title:All Departments
2011-11-21 15:33:03.309 ProjectTM[28209:f803] title:Eng
2011-11-21 15:33:03.312 ProjectTM[28209:f803] titleeck
2011-11-21 15:33:03.314 ProjectTM[28209:f803] title:Interior
2011-11-21 15:33:03.316 ProjectTM[28209:f803] title:Bridge
2011-11-21 15:33:03.317 ProjectTM[28209:f803] parse complete
2011-11-21 15:33:03.318 ProjectTM[28209:f803] accountData.departments: 0
r@t is offline   Reply With Quote
Old 11-21-2011, 09:04 AM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Are you sure your 'mArray' contains anything? Where and how is it declared? Put a breakpoint on 'mArray addObject' call in parser:didEndElement to see if anything is actually added to it.
baja_yu is offline   Reply With Quote
Old 11-21-2011, 09:16 AM   #3 (permalink)
Registered Member
 
ebender001's Avatar
 
Join Date: Mar 2010
Location: Missouri
Age: 57
Posts: 70
ebender001 is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Are you sure your 'mArray' contains anything? Where and how is it declared? Put a breakpoint on 'mArray addObject' call in parser:didEndElement to see if anything is actually added to it.
Usual problem is failure to instantiate your mutable array, therefore sending messages to nil.

Ed
ebender001 is offline   Reply With Quote
Old 11-21-2011, 09:48 AM   #4 (permalink)
r@t
Registered Member
 
Join Date: Jan 2011
Posts: 21
r@t is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Are you sure your 'mArray' contains anything? Where and how is it declared? Put a breakpoint on 'mArray addObject' call in parser:didEndElement to see if anything is actually added to it.
Thanks for lightening response ... mArray is @synthesized ... judging by my new NSLogs nothing is going in to mArray:

Code:
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    
        //NSLog(@"ended element: %@", elementName);
	if ([elementName isEqualToString:@"z:row"]) {
        
        [mArray addObject:currentTitle];
         NSLog(@"mArray:%d",[mArray count]);     
    }
    
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
	
	//[activityIndicator stopAnimating];
	//[activityIndicator removeFromSuperview];
    
    NSMutableArray *tempDepartments = [[NSMutableArray alloc] initWithArray:mArray];
    accountData.departments = tempDepartments;
    [tempDepartments release];
	
	NSLog(@"parse complete");
    NSLog(@"mArray total:%d",[mArray count]);
    NSLog(@"accountData.departments: %d", [accountData.departments count]);
    
}
</listitems></GetListItemsResult></GetListItemsResponse></soap:Body></soap:Envelope>
2011-11-21 16:41:03.099 ProjectTM[28613:f803] title:All Departments
2011-11-21 16:41:03.100 ProjectTM[28613:f803] mArray:0
2011-11-21 16:41:03.101 ProjectTM[28613:f803] title:Eng
2011-11-21 16:41:03.102 ProjectTM[28613:f803] mArray:0
2011-11-21 16:41:03.102 ProjectTM[28613:f803] titleeck
2011-11-21 16:41:03.145 ProjectTM[28613:f803] mArray:0
2011-11-21 16:41:03.145 ProjectTM[28613:f803] title:Interior
2011-11-21 16:41:03.145 ProjectTM[28613:f803] mArray:0
2011-11-21 16:41:03.146 ProjectTM[28613:f803] title:Bridge
2011-11-21 16:41:03.146 ProjectTM[28613:f803] mArray:0
2011-11-21 16:41:03.167 ProjectTM[28613:f803] parse complete
2011-11-21 16:41:03.168 ProjectTM[28613:f803] mArray total:0
2011-11-21 16:41:03.169 ProjectTM[28613:f803] accountData.departments: 0
r@t is offline   Reply With Quote
Old 11-21-2011, 09:55 AM   #5 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

It's not enough to just @synthesize. That only creates getter and/or setter methods. The object still needs to be alloc/init-ed.
baja_yu is offline   Reply With Quote
Old 11-21-2011, 10:25 AM   #6 (permalink)
r@t
Registered Member
 
Join Date: Jan 2011
Posts: 21
r@t is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
It's not enough to just @synthesize. That only creates getter and/or setter methods. The object still needs to be alloc/init-ed.
Thanks baja_yu...

Last edited by r@t; 11-21-2011 at 11:16 AM.
r@t is offline   Reply With Quote
Old 11-21-2011, 11:13 AM   #7 (permalink)
r@t
Registered Member
 
Join Date: Jan 2011
Posts: 21
r@t is on a distinguished road
Default

Quote:
Originally Posted by r@t View Post
Thanks baja_yu... Is "mArray = [[NSMutableArray alloc]initWithObjects:currentTitle, nil]; " correct? If so where should i put it to stop it overwriting itself?
Thank you for your help .. all is good
r@t is offline   Reply With Quote
Old 11-21-2011, 11:21 AM   #8 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

That's right. Just remember that if an array was previously set to myArray, you need to release it before allocing a new one, and you need to release it in dealloc as well. As for where to create the array, it depends on how you want your parser to work, do you want to keep adding to the array with each parse, in which case you would just create the array once (viewDidLoad or similar), but if you want the array to hold just the items from the last parse, you would create the array (or at least empty the current one) each time before you start parsing.
baja_yu is offline   Reply With Quote
Reply

Bookmarks

Tags
nsmutablearray, nsxmlparser

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 392
14 members and 378 guests
7twenty7, chiataytuday, Clouds, dedeys78, Duncan C, e2applets, EvilElf, iekei, ipodphone, jeroenkeij, leostc, mbadegree, Murphy, QuantumDoja
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:33 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0