0

Why does initializing a parser...

  • in a TVC instead of the app delegate
  • writing the parsed values directly to the parser delegate, declared in .h:

    @interface TVC:UITableViewController{
    NSMutableArray *parsedDataArray;
    }
    @property (nonatomic, weak) NSMutableArray *parsedDataArray;
    

...result in a NULL value breakpoint when trying to use the parsed values in said TVC? like so:

NSDictionary *topicsDict = [NSDictionary dictionaryWithObject:parsedResult forKey:@"News"];

and when breakpoints are turned off, we get this in the output:

2013-05-31 01:38:28.256 app[2383:c07] parsedDataArray: (null)
2013-05-31 01:38:28.257 app[2383:c07] *** Terminating app due to uncaught exception   
 'NSInvalidArgumentException', reason: '*** 
-[__NSPlaceholderDictionary initWithObjects:forKeys:count:]:
attempt to insert nil object from objects[0]'
*** First throw call stack:
(0x16f4012 0x14f8e7e 0x16baa95 0x16c8946 0x7943 0x51e1c7 0x51e232 0x51e4da 0x5358e5
 0x5359cb 0x535c76 0x535d71 0x53689b 0x5369b9 0x536a45 0x63c20b 0x48d2dd 0x150c6b0 
0x121fc0 0x11633c 0x121eaf 0x52c2bd 0x474b56 0x47366f 0x473589 0x4727e4 0x47261e 
0x4733d9 0x4762d2 0x52099c 0x46d574 0x46d76f 0x46d905 0x476917 0x4976 0x43a157 
0x43a747 0x43b94b 0x44ccb5 0x44dbeb 0x43f698 0x2618df9 0x2618ad0 0x1669bf5 0x1669962
 0x169abb6 0x1699f44 0x1699e1b 0x43b17a 0x43cffc 0x464d 0x1f85)
libc++abi.dylib: terminate called throwing an exception

Parser.m

 - (Parser *) initXMLParser {

    TVC *tvc = [[TVC alloc] init];          

    NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"categorial" ofType:@"xml"];

    NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];

    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:xmlData];

    [xmlParser setDelegate:tvc];

 return self;

The following is how I allocate the parser array tvc.parsedDataArray = [[NSMutableArray alloc] init];

... They are also synthesized in the implementation of TVC...

If there is anything else I should know or share with you to figure this out, let me know.

Morkrom
  • 578
  • 7
  • 26
  • Is anything keeping the instance of XMLTableViewParser "parser" alive while the XML is being parsed? Why are you trying to use 2 objects like this? – Wain May 30 '13 at 17:17
  • I don't exactly know what you mean by keeping the parser class alive while the XML is being parsed. . . isn't the parser "alive" when its parsing? Also, what two objects are you referring to? – Morkrom May 30 '13 at 18:01
  • I assume you're using ARC. If you instantiate the parser and then don't hold any strong references to it, ARC will deallocate it. Setting it as the XML parser delegate is not a strong reference so I expect it's being deallocated straight away. – Wain May 30 '13 at 18:03
  • I am using ARC. OK, what you say makes sense, thank you for your thoughts. So how do you think is the best way to make a strong reference in this case? – Morkrom May 30 '13 at 18:06
  • Do you really need to use 2 objects? Make the TVC class the delegate of the NSXMLParser or create a strong @property to hold a reference to the parser. – Wain May 30 '13 at 18:09
  • When you say objects you you mean *xmlParser and *parser? So you basically recommend: scratch *xmlParser, change [xmlParser setDelegate:parser]; to [parser setDelegate:self];? – Morkrom May 30 '13 at 20:54
  • It depends what else the class is doing, but generally, yes. The second object seems like overkill. But adding the property should sort the problem. – Wain May 30 '13 at 20:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30938/discussion-between-morkrom-and-wain) – Morkrom May 30 '13 at 21:27

0 Answers0