Tutorial :Problem with If statements



Question:

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.


Solution:1

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:.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »