开发者

Objective-C XML Parsing

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:@"开发者_Go百科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 :)


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];
...


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.


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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜