I’m trying to add elements of a specific name to an NSMutableArray, which I’m getting from an XML file. I’m after the element. The first NSLog here outputs correctly:
Found an element named: image with a value of: http://media.giantbomb.com/uploads/1/10353/2320941-untitledb_tiny.jpg
The second NSLog outputs correctly:
I'm about to try adding a game called Bayonetta 2 to the array
The third NSLog always tells me that there are zero entries in the array. I didn’t get any error on the [_listOfGameNames addObject: element]; line, so I have no idea why it’s not adding it to the array.
Any ideas for me? Here’s the code in full:
- (void) searchWithString: (NSString *)string {
NSString *searchString = [NSString stringWithFormat: @"http://api.giantbomb.com/search/?api_key=MYKEY12345BLAHBLAH&resources=game&format=xml&query=%@", string];
NSURL *searchURL = [NSURL URLWithString: searchString];
parser = [[NSXMLParser alloc] initWithContentsOfURL: searchURL];
[parser setDelegate:self];
[parser parse];
// NSLog(@"XMLParser just reached the end of its initWithString method. The string was %@", searchURL);
NSLog(@"There are %u elements in the List Of Game Names array", [_listOfGameNames count]);
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
element = [NSMutableString string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
NSLog(@"Found an element named: %@ with a value of: %@", elementName, element);
if ([elementName isEqualToString:@"name"]) {
NSLog(@"I'm about to try adding a game called %@ to the array", element);
[_listOfGameNames addObject: element];
NSLog(@"I added an element to the array, making a total of %u", [_listOfGameNames count]);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(element == nil) element = [[NSMutableString alloc] init];
[element appendString:string];
}
You need to init the array. and you can implement the Getter.