NSArray gives NSZombie error - iphone

I am trying to pass a single array object (that is a nsdictionary of several values) back to my main view.
basicly when I set the view up I parse some xml into an array of dictionaries. I then set up my tableview with one of the values inside the NSdictionary, this value is also used to set up the alphabetical scroller and section titles. (this is done in a method I created)
At the end of that method I call [self.tableView reloadData]; every thing loads up perfectly and everything displays fine.
Now what I am trying to do is set it up so that when a cell is selected, I check the value inside the cell.textlabel and use that as a predicate to check against my array of dictionaries once It finds the corresponding entry I want to pass that dictionary up to the main view with a delegate I have made.
however I am getting a error, that I think might be happening due to my reloadData.. but am not sure.
This is what my predicate looks like.
NSPredicate *pred = [NSPredicate predicateWithFormat:#"%K like %#",#"MANUFACTURER",cell.textLabel.text];
NSArray *filter = [myDataArray filteredArrayUsingPredicate:pred]; //error happens here
//check to see if the value is the correct one
NSLog(#"My Filtered array = %#", filter);
//once problem has been found set up the delegate here.
and this is the error message I receive.
2011-10-31 10:43:57.333 code[5812:207] *** -[__NSArrayM filteredArrayUsingPredicate:]: message sent to deallocated instance 0x6874210
myDataArray is created in the NSXMLParser delegates as listed below.
//.h
NSMutableArray *myDataArray;
}
#property (nonatomic, retain) NSMutableArray *myDataArray;
//.m
#pragma mark - Parsing lifecycle
- (void)startTheParsingProcess:(NSData *)parserData
{
//myDataArray = [NSMutableArray arrayWithCapacity:8]; // not even sure if this is needed as its declared later on.
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:parserData]; //parserData passed to NSXMLParser delegate which starts the parsing process
[parser setDelegate:self];
[parser parse]; // starts the event-driven parsing operation.
[parser release];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:#"Row"])
{
manufactureMutableDictionary = [[NSMutableDictionary alloc] initWithDictionary:attributeDict];
}
if([elementName isEqualToString:#"Rows"])
{
myDataArray = [NSMutableArray arrayWithCapacity:8];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:#"Row"])
{
[myDataArray addObject:manufactureMutableDictionary];
}
[manufactureMutableDictionary release];
manufactureMutableDictionary = nil;
}
Any help would be greatly appreciated, also do you think I am going about passing all the values of the dictionary the right way?

You are using an autoreleased array
myDataArray = [NSMutableArray arrayWithCapacity:8];
You have properties set up so use them e.g.
self.myDataArray = [NSMutableArray arrayWithCapacity:8];
or even better
NSMutableArray *tmpMyDataArray = [[NSMutableArray alloc] initWithCapacity:8];
self.myDataArray = tmpMyDataArray;
[tmpMyDataArray release]; tmpMyDataArray = nil;

Related

Parse Nested XML Objective C - NSXMLParser

All,
I have XML in the following format:
<linked-list>
<Description>
<desc></desc>
<IP></IP>
</Description>
</linked-list>
This XML statement could have an infinite number of <Description></Description> inside of the <linked-list></linked-list>.
How should I parse this using NSXMLParser? My current code is as follows, but it parses incorrectly.
#implementation XMLParser
#synthesize response;
- (XMLParser *) initXMLParser
{
self = [super init];
// init dictionary of response data
response = [[NSMutableDictionary alloc] init];
return self;
}
//Gets Start Element of SessionData
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:#"linked-list"])
{
NSLog(#"Found linked-list in the return XML! Continuing...");
//response is a NSMutableArray instance variable
//THIS SHOULD NEVER NEED TO BE USED
if (!response)//if array is empty, it makes it!
{
NSLog(#"Dictionary is empty for some reason, creating...");
response = [[NSMutableDictionary alloc] init];
}
//END: THIS SHOULD NEVER BE USED
return;
}
else
{
currentElementName = elementName;
NSLog(#"Current Element Name = %#", currentElementName);
return;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (!currentElementValue) {
// init the ad hoc string with the value
currentElementValue = [[NSMutableString alloc] initWithString:string];
} else {
[currentElementValue setString:string];
NSLog(#"Processing value for : %#", string);
}
}
//Gets End Element of linked-list
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"linked-list"])
{
// We reached the end of the XML document
// dumps dictionary into log
NSLog(#"Dump:%#", [response description]);
return;
}
else
{
//Adds key and object to dictionary
[response setObject:currentElementValue forKey:currentElementName];
NSLog(#"Set values, going around again... brb.");
}
currentElementValue = nil;
currentElementName = nil;
}
#end
Some observations:
An infinite number of WHAT inside of the WHAT?
Assuming there can be more than one Description element, the outer data structure in which you store the contents must be a NSMutableArray, not a dictionary. You then use one mutable dictionary per Description element.
Consequently, in didStartElement:, check if the element name is #"Description" and if so, create a new NSMutableDictionary instance that you store in an ivar.
In foundCharacters:, you always have to append the new characters to the existing currentElementValue because the method can be called multiple times for each element's contents. I see many people do this wrong despite the fact that Apple's sample code clearly demonstrates the correct way.
In didEndElement:, do this:
If the element name is #"desc" or #"IP", assign currentElementValue to the corresponding key in your current mutable dictionary. Don't forget to release currentElementValue before you set it to nil. You currently have a memory leak in your code because you're not doing that.
If the element name is #"Description", add the current mutable dictionary to the mutable array. Release the dictionary and set the ivar to nil. A new dictionary will be created the next time you encounter a #"Description" element in didStartElement:.
If the element name is #"linked-list", the mutable array will contain all the dictionaries and you're done.

NSXMLParser Parsing RSS into a Custom Object

I have my nsxmlparser parsing the news feed just fine:
http://www.skysports.com/rss/0,20514,12433,00.xml
However when it comes to saving it into my custom object, although I recieve output of each entry in the xml, it only stores one record which happens to be the last one.
Please see my code:
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:#"rss"]) {
currentNews = [[NewsParse alloc] init];
}
}
-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"description"]) {
currentNews.newsTitle = currentNodeContent;
NSLog(#"description = %#",currentNodeContent);
}
if([elementName isEqualToString:#"rss"])
{
[news addObject:currentNews];
[currentNews release];
currentNews = nil;
[currentNodeContent release];
currentNodeContent = nil;
}
}
This method worked fine with a twitter feed but now i'm assuming because the xml is formed differently I cannot get it to work.
I'm still pretty new to using NSXMLParser so any help would be cool :)
Looking at the RSS feed it appears that you are looking for the wrong tag to begin and end your objects. You need to replace
[elementName isEqualToString:#"rss"]
with
[elementName isEqualToString:#"item"]
in both places.
The way you are doing it now you are looking at the entire page as 1 object. You need to look at each "item" ( <item> </item> ) as an object. The reason why you are successfully getting the last object to save is because you replacing each "description" every time you run through your items. It is replacing the string over and over and over again before you actually save. The last object never gets replaced before saving and therefor the only object you see as saved..
Hmmm, currentNews.newsTitle is changed every time and once it reads the closing rss tag, it has the value of the last feed. What type of object is your currentNews? If you want to keep all different titles, then you have to add them to some sort of object that holds several values, like an array, when you find the closing tag of description simply copy the string for currentNodeContent into an individual instance of news.
I would actually allocate the instance of currentNews in the didEndElement tag outside any comparisson just in case you have more elements to look for. That closing tag probably like this:
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"title"]) {
self.currentNews.newsTitle = [[NSString alloc] initWithString:currentNodeContent];
}
if ([elementName isEqualToString:#"description"]) {
self.currentNews.newsDescription = [[NSString alloc] initWithString:currentNodeContent];
}
if ([elementName isEqqualToString:#"link"]) {
self.currentNews.newsLink = [[NSString alloc] initWithString:currentNodeContent];
}
if ([elementName isEqualToString:#"guid"]) {
self.currentNews.newsGuid = [[NSString alloc] initWithString:currentNodeContent];
}
if ([elementName isEqualToString:#"pubDate"]) {
self.currentNews.newsPubDate = "probably a date formatter here";
}
if ([elementName isEqualToString:#"cathegory"]) {
self.currentNews.newsCathegory = [[NSString alloc] initWithString:currentNodeContent];
}
blah...
blah...
if ([elementName isEqualToString:#"item"]) {
[news addObject:currentNews];
[self.currentNews.newsTitle release];
[self.currentNews.newsDescription release];
[self.currentNews.newsLink release];
[self.currentNews.newsGuid release];
blah...
blah...
blah...
}
}
And allocate your currentNews object in your init method (remove it from your didStartElement) and release it in your dealloc method. Oh, and #Louie is right, you need to look at your news as an array object which obviously has several currentNews, this currentNews or item is what your parser should be concerned about. After parsing all the element in "just one item" you add it to your news array when it reads the last element tag for that "one item", because after that your parser is simply going to loop and look for the next item.

problem in copy string for other class

In my app I am sending a request to the web server and getting some results, then I am parsing this result and storing this result string into other class object string.
For example:
#interface GraphView : UIViewController<UITabBarControllerDelegate> {
NSMutableString *graphTempString;
}
#property(nonatomic,retain) NSMutableString *graphTempString;
#end
TSUClass .m
implementing NSURLConnection(),
connectionDidFinishLoading(),
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName
namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName
attributes:(NSDictionary *) attributeDict()
-(void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if( [elementName isEqualToString:#"statusTEResult"])
{
tempString=soapResults;
if (grpahCall == YES) {
graphViewController=[[GraphView alloc]init];
graphViewController.graphTempString=tempString;
[self.navigationController pushViewController:graphViewController animated:YES];
}
When I am debugging I can see the value of graphViewController.graphTempString but after going to GraphView, I am not able to see the values.
I hope some one know how to solve this issue.
Easy way to pass value.
[[NSUserDefaults standardUserDefaults]setObject:resultString forKey:#"resultString"];
[[NSUserDefaults standardUserDefaults] synchronize];
and in another class use this value like this
NSString *resultString=[[NSUserDefaults standardUserDefaults]valueForKey:#"resultString"];
or u may do it like this.
graphViewController.graphTempString=self.tempString;
can you try
2nClassObj.tempString =[NSString stringWithFormat:#"%#",resultString];
good luck
#Pooja i suggest you to try doing this by making a variable in AppDelegate class give your resultString to this variable and then fetch the value from this variable in your 2ndClass or in any class you want .....you will never get null value.
Lets suppose the object of your AppDelegate class is appDelegate and the variable which of NSString type is lets stringValue. Then in your class where you are getting the resultString do like this.
appDelegate.stringValue = resultString;
And in your 2ndClass class take the value from this variable like this.
tempString = appDelegate.stringValue;
You will get the data.....Hope you got my point.
Good Luck!
What happens if you add some NSLog statements?
if (grpahCall == YES) {
graphViewController=[[GraphView alloc]init];
NSLog(#"-%#-", tempString);
graphViewController.graphTempString=tempString;
NSLog(#"-%#-", graphViewcontroller.graphTempString);
[self.navigationController pushViewController:graphViewController animated:YES];
}
I would expect to see the same thing output twice? Can you tell me what you get in the console?

Memory Leak with NSXMLParser on IPhone

below is my code, Leaks says I am getting a memory leak around NSMutableString alloc method. I am sure it is something I simply overlooked, let me know if anyone has any thoughts. Thanks!
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if (!currentValue) {
currentValue = [[NSMutableString alloc] initWithCapacity:[string length]];
}
[currentValue setString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if([elementName isEqualToString:#"phone"]){
currentAgent.phone = currentValue;
}
[currentValue release];
currentValue = nil;
}
-Agent is a custom object that was created when the class was initialized. The XML is valid and has all the appropriate begin/end tags.
Looking over this code, I think it's more likely that your Agent class is leaking phone. Assuming Agent uses retain for the phone property, this will cause the phone to persist longer than it should.
The creator of the object gets "credited" with the leak, even if the extra retain is somewhere else.
In other words, in Agent:
- (void)dealloc {
self.phone = nil;
// anything else you need to do
[super dealloc];
}

Help! How to share NSXMLParser class between controllers?

I've read posts about this, and it seems pretty straight-forward. I'm pretty new to Obj-C and iPhone dev in general, so I could easily be overlooking something. I can't seem to return the NSMutableArray with the Article objects. I don't get any errors, but when I try to NSLog() some stuff I'm getting EXEC_BAD_ACCESS errors (I'm assuming a memory access issue?). I have an ArticlesParser class that does the parsing... Here's what it looks like:
// ArticlesParser.h
#import <Foundation/Foundation.h>
#import "Article.h"
#class Article;
#interface ArticlesParser : NSObject <NSXMLParserDelegate> {
NSMutableString *currentCharaters;
Article *currentArticle;
NSMutableArray *articlesCollection;
NSMutableData *xmlData;
NSURLConnection *connectionInProgress;
BOOL connectionHasCompleted;
}
#property (nonatomic, assign) BOOL connectionHasCompleted;
- (void)parseUrl:(NSString *)url;
- (void)beginParsing:(NSURL *)xmlUrl;
- (NSMutableArray *)arrayOfArticles;
#end
Here's the implementation...
// ArticlesParser.m
#import "ArticlesParser.h"
#implementation ArticlesParser
#synthesize connectionHasCompleted;
#pragma mark -
#pragma mark Parsing methods
- (void)parseUrl:(NSString *)url
{
[self setConnectionHasCompleted:NO];
NSURL *xmlUrl = [NSURL URLWithString:url];
[self beginParsing:xmlUrl];
}
- (void)beginParsing:(NSURL *)xmlUrl
{
[articlesCollection removeAllObjects];
articlesCollection = [[NSMutableArray alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:xmlUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
// clear existing connection if there is one
if (connectionInProgress) {
[connectionInProgress cancel];
[connectionInProgress release];
}
[xmlData release];
xmlData = [[NSMutableData alloc] init];
// asynchronous connection
connectionInProgress = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
- (NSMutableArray *)arrayOfArticles
{
// NOT RETURNING ANYTHING
return articlesCollection;
}
#pragma mark -
#pragma mark NSXMLParserDelegate methods
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[xmlData appendData:data];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqual:#"article"]) {
currentArticle = [[Article alloc] init];
return;
}
if ([elementName isEqual:#"title"]) {
currentCharaters = [[NSMutableString alloc] init];
return;
}
if ([elementName isEqual:#"last_updated"]) {
currentCharaters = [[NSMutableString alloc] init];
return;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[currentCharaters appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqual:#"article"]) {
[articlesCollection addObject:currentArticle];
[currentArticle release], currentArticle = nil;
return;
}
if ([elementName isEqual:#"title"]) {
[currentArticle setTitle:currentCharaters];
[currentCharaters release], currentCharaters = nil;
return;
}
if ([elementName isEqual:#"last_updated"]) {
[currentArticle setLastModified:currentCharaters];
[currentCharaters release], currentCharaters = nil;
return;
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
[parser setDelegate:self];
[parser parse];
[parser release];
[self setConnectionHasCompleted:YES];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[currentArticle release];
currentArticle = nil;
[currentCharaters release];
currentCharaters = nil;
[articlesCollection release];
articlesCollection = nil;
[connectionInProgress release];
connectionInProgress = nil;
[xmlData release];
xmlData = nil;
NSLog(#"connection failed: %#", [error localizedDescription]);
}
#end
I know that the actual parsing works because I did have this directly in my view controller and everything worked fine. But now I want to access basically the same thing from another controller, only the URL is different (returns the same formatted XML though).
Here's how I'm trying to make use of this class in my controller:
// instance method called within an articles controller
// that is to load the results in a table view
- (void)loadArticles
{
// (leaving off the URL because it's not important)
NSString *urlToRequest = [NSString stringWithFormat:#"...", [self letterToList]];
ArticlesParser *aParser = [[ArticlesParser alloc] init];
// initiate the parsing
[aParser parseUrl:urlToRequest];
// load up the articles ivar so the tableview can
// make use of it to load its cells
articles = [aParser arrayOfArticles];
}
Is there something obvious that I'm missing? Is this even a good way to share the NSXMLParser code?
I'm pulling my hair out over this one... thanks in advance!
What is it you're trying to NSLog that generates the EXEC_BAD_ACCESS error? Looking at your code your call to arrayOfArticles should return an NSMutableArray with no elements, so e.g. something like this would understandably give an EXEC_BAD_ACCESS:
NSLog(#"%#", [[articles objectAtIndex:0] description]); // index out of bounds
By having your XML parser class also responsible for fetching the data it's going to parse (using NSURLConnection) you've made it asynchronous, which means it's no longer suitable to be used like this:
ArticlesParser *ap = [[[ArticlesParser alloc] init] autorelease];
[ap parseURL:#"http://example.com/foo"];
NSArray *anArray = [ap arrayOfArticles];
anArray is now an empty array, and will only be populated at some indeterminate point in the future, if at all - and you can't detect when that time comes without polling the array. Urgh! :)
There are a couple of ways you might get around this. One approach is to have your XML Parser class declare delegate methods, offering callbacks for when the XML data has been fully fetched and parsed and when error conditions occur (in much the same way that the delegate methods in NSURLConnection work). Another approach is to have your XML Parser class be a simple (synchronous) XML parser, and move the asynchronous data-fetching code to outside your class.
There are a few things I see a problem with off the top of my head.
First, you need to either copy or retain the return from arrayOfArticles if you're going to hang on to it and use it later.
articles = [[aParser arrayOfArticles] copy];
You then of course need to make sure you release it later whenever it's appropriate.
Second, as it's written loadArticles actually leaks the ArticleParser it creates so you need to call [aParser release] at the end of the method.
The fact that you have to release the parser, which created the array, is what makes it necessary to retain/copy the return value. As soon as the ArticlesParser is deallocated it'll release it's internal articlesCollection and deallocate it if that was the last reference. Since your arrayOfArticles method hands out that reference to others, they need to copy the array or retain the reference to keep it alive after the ArticlesParser that created it dies.
Finally, you're downloading the data asynchronously, but you're calling arrayOfArticles immediately after calling parseUrl:. This is never going to result in you getting anything useful because no data has been downloaded or parsed yet. You need your ArticlesParser to provide some way to notify interested parties when it is done parsing the downloaded data and THEN they can call arrayOfArticles to get the data.
EDIT:
One way to deal with the notification would be to create a delegate protocol, add a delegate property to ArticlesParser, have the controller set itself as the value of that property, and have the parser call the delegate's method when it's done.
For example:
// ArticlesParser.h
#import <Foundation/Foundation.h>
#import "Article.h"
#class Article;
#class ArticlesParser;
#protocol ArticlesParserDelegate <NSObject> {
- (void)parserDidFinish:(ArticlesParser*)parser;
- (void)parser:(ArticlesParser*)parser didFailWithError:(NSError*)error;
#end
#interface ArticlesParser : NSObject <NSXMLParserDelegate> {
id<ArticlesParserDelegate> delegate;
// ... the rest the same ...
}
#property (nonatomic, assign) id<ArticlesParserDelegate> delegate;
// ... the rest the same ...
#end
// ArticlesParser.m
// ... the same as you have, but with this stuff added ...
#synthesize delegate;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// ... same as you have, add this at end...
[delegate parserDidFinish:self];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// ... same as you have, add this at end...
[delegate parser:self didFailWithError:error];
}