0

I am building a media scrobbler. What I want the program to do is to detect the media from iTunes/MPlayer and have the program send an update via API. I got that part down, but when the same Media Title and Segment (track/episode) is compared to this If statement, it does the same action over again, which I don't want the program to do.

Here is the problematic code that I'm going through:

    if ([[segment stringValue] length] == 0 || [[mediatitle stringValue]length] == 0 ) {
        // Do Nothing
    }
    else if ([mediatitle stringValue] == ScrobbledMediaTitle && [segment stringValue] == ScrobbledMediaSegment && scrobblesuccess == 1) {
        // Do Nothing
        }
    else {
        int httperror = [self scrobble];
        switch (httperror) {
            case 200:
                [scrobblestatus setObjectValue:@"Scrobble Successful..."];
                [GrowlApplicationBridge notifyWithTitle:@"Scrobble Successful"
                                            description:[NSString stringWithFormat:@"%@ - %@", [mediatitle stringValue], [segment stringValue]] 
                                       notificationName:@"Message"
                                               iconData:nil
                                               priority:0
                                               isSticky:NO
                                           clickContext:[NSDate date]];
                ScrobbledMediaTitle = [mediatitle stringValue];
                ScrobbledMediaSegment = [segment stringValue];
                scrobblesuccess = YES;
                //Set up Delegate
                Melative_ExampleAppDelegate* appDelegate=[NSApp delegate];
                //Set last successful scrobble to statusItem Tooltip
                [appDelegate setStatusToolTip:[NSString stringWithFormat:@"MelScrobbleX - Last Scrobble: %@ - %@", [mediatitle stringValue], [segment stringValue]]];               
                NSLog(@"ScrobbledMediaTitle = %@", ScrobbledMediaTitle);
                NSLog(@"ScrobbledMediaSegment = %@" , ScrobbledMediaSegment);
                NSLog(@"BOOL = %d", (int)scrobblesuccess);              
                break;
            case 401:
                // Set Status
                [scrobblestatus setObjectValue:@"Unable to Scrobble..."];
                [GrowlApplicationBridge notifyWithTitle:@"Scrobble Unsuccessful"
                                            description:@"Check your login information and try scrobbling again." 
                                       notificationName:@"Message"
                                               iconData:nil
                                               priority:0
                                               isSticky:NO
                                           clickContext:[NSDate date]];
                scrobblesuccess = NO;
                break;
            default:
                // Set Status
                [scrobblestatus setObjectValue:@"Unable to Scrobble..."];
                [GrowlApplicationBridge notifyWithTitle:@"Scrobble Unsuccessful"
                                            description:[NSString stringWithFormat:@"Unknown Error. Error %i", httperror]
                                       notificationName:@"Message"
                                               iconData:nil
                                               priority:0
                                               isSticky:NO
                                           clickContext:[NSDate date]];
                scrobblesuccess = NO;
                break;              
        }

}
}

I try figuring out with the NSLog output and this is what I get:

    2010-08-01 21:58:56.935 MelScrobbleX[7775:a0f] ScrobbledMediaTitle = (null)
2010-08-01 21:58:56.935 MelScrobbleX[7775:a0f] ScrobbledMediaSegment = (null)
2010-08-01 21:58:56.935 MelScrobbleX[7775:a0f] BOOL = 0
2010-08-01 21:58:56.935 MelScrobbleX[7775:a0f] mediatitle = Lia COLLECTION ALBUM "SPECTRUM RAYS"
2010-08-01 21:58:56.936 MelScrobbleX[7775:a0f] mediasegment = Tori no Uta -StripE REMIX-
2010-08-01 21:58:56.936 MelScrobbleX[7775:a0f] Scrobbled
2010-08-01 21:58:56.936 MelScrobbleX[7775:a0f] ScrobbledMediaTitle = Lia COLLECTION ALBUM "SPECTRUM RAYS"
2010-08-01 21:58:56.936 MelScrobbleX[7775:a0f] ScrobbledMediaSegment = Tori no Uta -StripE REMIX-
2010-08-01 21:58:56.936 MelScrobbleX[7775:a0f] BOOL = 1
2010-08-01 21:59:06.709 MelScrobbleX[7775:a0f] ScrobbledMediaTitle = Lia COLLECTION ALBUM "SPECTRUM RAYS"
2010-08-01 21:59:06.709 MelScrobbleX[7775:a0f] ScrobbledMediaSegment = Tori no Uta -StripE REMIX-
2010-08-01 21:59:06.710 MelScrobbleX[7775:a0f] BOOL = 1
2010-08-01 21:59:06.710 MelScrobbleX[7775:a0f] mediatitle = Lia COLLECTION ALBUM "SPECTRUM RAYS"
2010-08-01 21:59:06.710 MelScrobbleX[7775:a0f] mediasegment = Tori no Uta -StripE REMIX-
2010-08-01 21:59:06.711 MelScrobbleX[7775:a0f] Scrobbled
2010-08-01 21:59:06.711 MelScrobbleX[7775:a0f] ScrobbledMediaTitle = Lia COLLECTION ALBUM "SPECTRUM RAYS"
2010-08-01 21:59:06.711 MelScrobbleX[7775:a0f] ScrobbledMediaSegment = Tori no Uta -StripE REMIX-
2010-08-01 21:59:06.711 MelScrobbleX[7775:a0f] BOOL = 1

Can't figure out the reason why the IF statement wouldn't fire since the values are the same and scrobbleSuccess is true.

5
  • I just editted your post. Next time, please, format the code Commented Aug 2, 2010 at 2:10
  • Sorry about that... It didn't paste properly... >_< Commented Aug 2, 2010 at 2:12
  • It's always a good idea to group the clauses in the if statement even though precedence may be correct.....have you tried that? Commented Aug 2, 2010 at 2:20
  • I would suggest you split up your mega function into several smaller functions, that makes the code easier to read. Commented Aug 2, 2010 at 2:24
  • I managed to reduce the code. The Scrobble API action is now in a separate function and reports the HTTP code in the integer... Still need to see if that did anything to the IF statement. Commented Aug 2, 2010 at 2:34

1 Answer 1

3

Comparing objects with == compares pointer equality. If you want to see whether the objects have the same value (even if they exist in separate memory locations), use isEqual: or, in the case of NSString, isEqualToString:.

Sign up to request clarification or add additional context in comments.

1 Comment

That fixed the issue... I assumed that == worked with strings since it worked with integers and Boolean values (also because I came from Visual Basic), but I was wrong. I need to break those bad habits. :p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.