0

I'm new to Objective C and XML, so this is going to look pretty rough.

My code is as follows:

#import "dbCommunicator.h"
#import "BookInfo.h"
#import "TouchXML.h"

@implementation dbCommunicator

-(void)getNextBooks {
    if(self.bookListing == nil) {
        for(int i = 0; i < 5; i++) {
            BookInfo *newBook = [[BookInfo alloc] init];
            [self.bookListing addObject:newBook];
        }
        self.currentPage = 0;
    }
    if(self.currentPage == 10) {
        self.currentPage = 0;
    }

    NSString *test = @"<Authors><Book.Author><Id>1026</Id><Name>Mark Twain</Name></Book.Author></Authors>";
    NSString *bookQueryString = [NSString stringWithFormat:@"http://bookworm.azurewebsites.net/api/book/list/5/%d",_currentPage];
    NSURL *bookQueryURL = [NSURL URLWithString: bookQueryString];
    self.currentPage++;
    NSError *theError = NULL;
    NSDictionary *mappings = [NSDictionary dictionaryWithObjectsAndKeys:
                              @"http://schemas.datacontract.org/2004/07/Bookworm.Models.ResponseModels",
                              @"datacontract",
                              nil];
    CXMLDocument *xmlReturn = [[CXMLDocument alloc] initWithXMLString:test options:0 error:&theError];
    NSArray *returnedBooks = [xmlReturn nodesForXPath:@"//Authors" error:&theError];
    for(CXMLElement *resultElement in returnedBooks) {
        NSLog(@"%s", "We actually got here");
    }
}

There's a lot of junk code in there at the moment. The intention is to pull an XML file from a database and put its information into an array of BookInfo classes. For the moment, I simplified by just using a test XMLstring to ensure it wasn't an issue with what the database was sending me. This makes the dictionary (to deal with the namespace issues TouchXML has) superfluous. Anyways.

It always crashes with a unrecognized selector error on this line:

[theArray addObject:[CXMLNode nodeWithLibXMLNode:theNode freeOnDealloc:NO]];

In this context:

if (xmlXPathNodeSetIsEmpty(theXPathObject->nodesetval))
    theResult = [NSArray array]; // TODO better to return NULL?
else {
    NSMutableArray *theArray = [NSMutableArray array];
    int N;
    for (N = 0; N < theXPathObject->nodesetval->nodeNr; N++) {
        xmlNodePtr theNode = theXPathObject->nodesetval->nodeTab[N];
        [theArray addObject:[CXMLNode nodeWithLibXMLNode:theNode freeOnDealloc:NO]];
    }
}

and with that, I'm totally at a loss. I've tried plenty of things, scoured every StackOverflow post even closely related and tried their fixes, nothing's working. Any suggestions?

Matteo Gobbi
  • 17,697
  • 3
  • 27
  • 41
Aphrodisizach
  • 39
  • 1
  • 5

2 Answers2

0

array is a static method on NSArray a super class of NSMutableArray that returns an NSArray object. NSArray does not respond to the method addObject:

Try instead NSMutableArray *theArray = [NSMutableArray new];

Joe
  • 3,664
  • 25
  • 27
  • Same issue, sadly. I had already tried changing it to `NSMutableArray *theArray = [[NSMutableArray alloc] init]`, which didn't work either. Edit: I guess I should also mention that that portion of the code is in TouchXML's source. – Aphrodisizach May 07 '14 at 22:10
0

The problem probably is on this selector:

nodeWithLibXMLNode:freeOnDealloc:

if you try to see your crash log in the console, should be written the selector unrecognized and should be this. If so check the class reference of the CXMLNode.

I checked now, and that method doesn't exists. Just a method might be useful for you in some way, that is:

- (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error;

Enjoy ;)

Matteo Gobbi
  • 17,697
  • 3
  • 27
  • 41
  • `nodeWithLibXMLNode:freeOnDealloc` is defined in CXMLNode_PrivateExtensions.m, which I didn't include a snippet of. I ended up just copying my files into a new project and it worked; I must have made a mistake with my header paths or something when setting up TouchXML the first time. – Aphrodisizach May 08 '14 at 23:34