I am very new to iPhone development, so please be gentle with me.
I have set up a web service which returns XML data. Sample data:
<categories>
<category>
<CategoryId>1</CategoryId>
<CategoryName>cat1</CategoryName>
</category>
<category>
<CategoryId>2</CategoryId>
<CategoryName>cat2</CategoryName>
</category>
</categories>
I was working on the code provided here: http://gigaom.com/apple/tutorial-build-a-simple-rss-reader-for-iphone/
And I’m getting an error.
Here is the function which sets the array:
-(void)parser: (NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"ended element: %@", elementName);
if([elementName isEqualToString:@"category"])
{
[category setObject:currentID forKey:@"CategoryId"];
[category setObject:currentName forKey:@"CategoryName"];
[categories addObject:[currentElement copy]];
}
}`-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"Found characters: %@", string);
if([currentElement isEqualToString:@"CategoryName"])
{
[currentName appendString:string];
}
else if([currentElement isEqualToString:@"CategoryId"])
{
[currentID appendString:string];
}
}
and I get an error when I try and read from the array:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
int categoryIndex = [indexPath indexAtPosition: [indexPath length] -1];
[cell.textLabel.text = [categories objectAtIndex: categoryIndex] objectForKey: @"CategoryName"];
return cell;
}
I get this error:
2011-06-05 11:33:35.436 FirstTry[2419:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x4c26b40'
I’m a little lost with the iOS dev thing. Thank you!
The reason for "unrecognized selector sent to instance …"-errors is that you are calling a function to an instance which doesn’t implement that function.
You assume the following structure of your data in
cellForRowAtIndexPath:usedLocalVariableName:Classcategories:NSArraywhich contains manycategory:NSMutableDictionarywhich contains the@"CategoryName"-Key with aNSStringvalue.Your Parse-Code results in sth different:
categories:NSArraywhich contains manycurrentElement:NSString(I assume thatcurrentElementis aNSStringbecause you are usingisEqualToString:-method oncurrentElement).So if you run that code the code-snippet:
[categories objectAtIndex: categoryIndex]will return aNSStringand not aNSDictionaryandNSStringdoes not implement theobjectForKey:-method.I am not sure (I’ve not tested it) but I think
should work. The autorelease is against memory-leaks
Another thing: You use
Why not using
int categoryIndex = indexPath.rowedit: You are using the NSXMLParser. I am using the TBXML-Parser which parses faster than NSXML and is more comfortable to use.