Elad Lachmi
Elad Lachmi

Reputation: 10561

iphone - web service returns XML

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!

Upvotes: 0

Views: 485

Answers (1)

thomas
thomas

Reputation: 5649

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:Class

categories:NSArray which contains many category:NSMutableDictionary which contains the @"CategoryName"-Key with a NSString value.

Your Parse-Code results in sth different:

categories:NSArraywhich contains many currentElement:NSString (I assume that currentElement is a NSString because you are using isEqualToString:-method on currentElement).

So if you run that code the code-snippet: [categories objectAtIndex: categoryIndex] will return a NSString and not a NSDictionary and NSString does not implement the objectForKey:-method.

I am not sure (I've not tested it) but I think

[categories addObject:[[category copy] autorelease]];

should work. The autorelease is against memory-leaks

Another thing: You use

int categoryIndex = [indexPath indexAtPosition: [indexPath length] -1];

Why not using int categoryIndex = indexPath.row

edit: You are using the NSXMLParser. I am using the TBXML-Parser which parses faster than NSXML and is more comfortable to use.

Upvotes: 1

Related Questions