views:

92

answers:

3

Hi,

I'm using this code to parse XML in the instance variable response:

@implementation Delegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
MKFacebook *fbConnection = [[MKFacebook facebookWithAPIKey:@"----" delegate:self] retain];
[fbConnection login];

NSMutableDictionary *parameters = [[[NSMutableDictionary alloc] init] autorelease];
MKFacebookRequest *request = [MKFacebookRequest requestWithDelegate:self];

//set up parameters for request
[parameters setValue:[NSArray arrayWithObjects:[fbConnection uid], @"123456789", nil] forKey:@"uids"];
[parameters setValue:[NSArray arrayWithObjects:@"first_name",@"last_name",nil] forKey:@"fields"];

//send the request
[request sendRequest:@"users.getInfo" withParameters:parameters];
}


-(void)userLoginSuccessful
{
NSLog(@"neat");
}


- (void)facebookRequest:(MKFacebookRequest *)request responseReceived:(NSString *)response
{
CFStringRef response = (CFStringRef)response;
NSData *xmlData = [[NSData alloc] initWithData:[response2 dataUsingEncoding:NSUTF8StringEncoding]];
NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:xmlData] autorelease];
[parser setDelegate:self];
[parser parse];     
}

But, I am getting this console error upon executing the code:

2010-08-12 20:24:46.924 App[2966:a0f] -[NSXMLDocument dataUsingEncoding:]: unrecognized selector sent to instance 0x47c250

Thanks in advance :)

A: 

Two thoughts:

  1. It's unnecessary to alloc/init a new data object. You can simply do: NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:response] autorelease];
  2. Put a breakpoint on objc_exception_throw and find out where that exception (the unrecognized selector) is coming from.
Dave DeLong
it says it coming from [parser parse]; any ideas?
James Eggers
A: 

Your problem is that xmlData isn't being initialized properly (I think):

NSData *xmlData = [[NSData alloc] initWithData:(id)response];

For some reason you're casting response as an id. initWithData expects to be passed an NSData object, therefore you should convert response to an NSData (which is currently an NSString) with something like:

NSData *xmlData = [[NSData alloc] initWithData:[response dataUsingEncoding:NSUTF8StringEncoding];
benofsky
getting the same error :( *so* annoying.
James Eggers
Have you stepped through it with Gdb? I don't think that code above is your problem, do you have a line number, so you're sure?
benofsky
'Variable is not a CFString', any idea what that means?http://skitch.com/james-eggers/dt3yk/error
James Eggers
perhaps whatever is calling that method is sending a CFString and not an NSString? Post more code above going back a few steps.
benofsky
Posted the code above
James Eggers
+1  A: 

It seems like you are implementing the delegate method incorrectly. According to the MKAbeFook documentation, the signature should be:

- (void)facebookRequest:(MKFacebookRequest *)request responseReceived:(id)response

where response is an NSArray or NSDictionary if you specified JSON response type. Since you seem to be getting an NSXMLDocument, it must be defaulting to XML response type. The XML is already parsed and turned in to a DOM, so you don't need to parse it. Just cast the response and go to work with the NSXML tree-based API.

NSXMLDocument *document = (NSXMLDocument *)response;
NSArray *someChildren = [document nodesForXPath:@"//foo" error:NULL];
...
Daniel Dickison
thanks so much, I think it's fixed :)
James Eggers