-2

While creating a login view on my iphone app, I ran into this error:

'NSInvalidArgumentException', reason: '-[SBJsonParser objectWithString:error:]:
    unrecognized selector sent to instance 0x6695330'

It's coming from this line:

NSDictionary *results = [parser objectWithString:json_string error:nil];

From this method:

+ (BOOL)loginWithUsername:(NSString *)username password:(NSString *)password
{
    NSString *urlString = [NSString stringWithFormat:@"%@login3", ROSE_ROOT_URL];
    NSURL *url = [NSURL URLWithString:urlString];


    NSString *requestString = [NSString stringWithFormat:@"&mobile=1&username=%@&password=%@", username, password];


    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[NSData dataWithBytes:[requestString UTF8String] length:[requestString length]]];

    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

    [request release];
    // parse the JSON response into an object
    // Here we're using NSArray since we're parsing an array of JSON product objects
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSDictionary *results = [parser objectWithString:json_string error:nil];

    [json_string release];
    [parser release];


    //  NSDictionary *results = [RoseFetcher fetch:request]; 
    //  [request release];

    if ([[results objectForKey:@"password"] intValue] == 0)
        return NO;

    return YES;
}

Any help or explanation would be greatly appreciated.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Stanwin Siow
  • 437
  • 1
  • 8
  • 21

1 Answers1

3

According to the documentation for SBJsonParser, it doesn't have a method called -objectWithString:error: - an opinion that's corroborated by your findings at runtime. Try sending it a message it will respond to, like -objectWithString:.

  • hmm i'll try it out. Thank you. I believe it's the non method objectWithString:error. Xcode is warning me about it. Thanks Graham! Will prob accept the answer once i figured it out completely. :) – Stanwin Siow Jan 10 '12 at 14:20
  • Graham you're right. When i removed the error:nil it went through. Thanks again! Will be accepting your answer. – Stanwin Siow Jan 10 '12 at 14:24
  • Worked for me too! Even though I can see in the code for SBJSON.m that there is a method for objectWithString:error: – Smikey Apr 27 '12 at 15:21
  • I just want to say that using the same json library in multiple places in my app using the exact same code, I only get this error in one place... Everywhere else it works just fine. I did take out the error portion as well, but wish there was an explaination as to why it "sometimes" doesn't work. – James Aug 05 '13 at 19:39