views:

199

answers:

2

What I'm trying to get is to search for the Anime Titile's ID, compare the length and perform some action afterwards. Here is what I get in the debugger:

2010-08-09 14:30:48.818 MAL Updater OS X[37415:a0f] Detected : Amagami SS - 06
2010-08-09 14:30:48.821 MAL Updater OS X[37415:a0f] http://mal-api.com/anime/search?q=Amagami%20SS
2010-08-09 14:30:49.635 MAL Updater OS X[37415:a0f] 8676
2010-08-09 14:30:49.636 MAL Updater OS X[37415:a0f] -[NSCFNumber length]: unrecognized selector sent to instance 0x384aa40
2010-08-09 14:30:49.637 MAL Updater OS X[37415:a0f] -[NSCFNumber length]: unrecognized selector sent to instance 0x384aa40

The code in question:

    if ([self detectmedia] == 1) { // Detects Media from MPlayer via LSOF
        NSLog(@"Detected : %@ - %@", DetectedTitle, DetectedEpisode);

        NSString * AniID = [self searchanime]; // Perform a Search Operation and Returns the ID of the time from JSON
        NSLog(@"%@",AniID);
        if (AniID.length > 0) { // Compare the length of AniID to make sure it contains a ID
        // Other Action here
        }
//Release Detected Title and Episode
        [DetectedTitle release];
        [DetectedEpisode release];
    }

SearchAnime method:

-(NSString *)searchanime{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //Escape Search Term
    NSString * searchterm = (NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                                                NULL,
                                                                                (CFStringRef)DetectedTitle,
                                                                                NULL,
                                                                                (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                                kCFStringEncodingUTF8 );

    //Set Search API
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://mal-api.com/anime/search?q=%@",searchterm]];
    NSLog(@"%@",[NSString stringWithFormat:@"http://mal-api.com/anime/search?q=%@",searchterm]);
    //Release searchterm
    [searchterm release];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    //Ignore Cookies
    [request setUseCookiePersistence:NO];
    //Set Token
    [request addRequestHeader:@"Authorization" value:[NSString stringWithFormat:@"Basic %@",[defaults objectForKey:@"Base64Token"]]];
    //Perform Search
    [request startSynchronous];
    // Get Status Code
    int statusCode = [request responseStatusCode];
            NSString *response = [request responseString];
    if (statusCode == 200 ) {
        return [self RegExSearchTitle:response]; // Returns ID as NSString
    }
    else {
        return @"";
    }

}

RegExSearchTitle

-(NSString *)RegExSearchTitle:(NSString *)ResponseData {
    OGRegularExpressionMatch    *match;
    OGRegularExpression    *regex;
    //Set Detected Anime Title
    regex = [OGRegularExpression regularExpressionWithString:DetectedTitle];
    NSEnumerator    *enumerator;
    // Initalize JSON parser
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSArray *searchdata = [parser objectWithString:ResponseData error:nil];
    for (id obj in searchdata) {
        // Look in every RegEx Entry until the extact title is found.
        enumerator = [regex matchEnumeratorInString:[obj objectForKey:@"title"]];
        while ((match = [enumerator nextObject]) != nil) {
            // Return the AniID for the matched title
            return [obj objectForKey:@"id"];
        }
    }
    // Nothing Found, return nothing
    return @"";
}

This behavior is unusual because I have compared the NSString's length in the past and it never failed on me. I am wondering, what is causing the problem?

A: 

Well, it's because you assigned an NSNumber to AniID, not an NSString. NSNumber doesn't have a length method.

Carl Norum
[self searchanime] reports back as a NSstring, which doesn't explain why the length function wouldn't work.
chikorita157
+2  A: 

The declared return type of RegExSearchTitle is NSString *, but that doesn’t force the returned object to actually be an NSString. The "id" element of obj (from the JSON) is a number, so an NSNumber is being returned. The compiler can’t warn you about this because it doesn’t know what classes will be found in a collection.

There are other bugs in the code. Having an unconditional return in a while statement in a for statement does not make sense.

On a side note, by convention Objective-C method names start with a lowercase letter.

Ahruman
Okay, that is the reason it didn't work, but I need to figure out the regex in getting the exact title. The solution was to create a new NSString variable (titleid), use titleid = [NSString stringwithformat:@"%@", [obj objectForKey:@"id"]] and return titleid, which is outside the for loop.I fixed the method names so they are proper.
chikorita157