If语句的问题

时间:2010-08-02 02:08:55

标签: iphone objective-c

我正在建立一个媒体scrobbler。我希望程序做的是从iTunes / MPlayer中检测媒体并让程序通过API发送更新。我得到了那个部分,但是当相同的媒体标题和片段(曲目/剧集)与这个If语句进行比较时,它会再次执行相同的操作,我不希望程序这样做。

以下是我正在经历的有问题的代码:

    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;              
        }

}
}

我尝试找出NSLog输出,这就是我得到的:

    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

无法弄清楚IF语句不会触发的原因,因为值相同且scrobbleSuccess为真。

1 个答案:

答案 0 :(得分:3)

将对象与==进行比较,比较指针相等性。如果要查看对象是否具有相同的值(即使它们存在于不同的内存位置),请使用isEqual:,或者对于NSString,使用isEqualToString: