Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my code

C#
//set button cliekd
- (IBAction)SetBtn:(id)sender {
     NSString *formattedDateString = [_dateformatter stringFromDate:_datepicker.dateValue];
    _settimelabel.stringValue = formattedDateString;
}


//timer ticking
-(void) onTick {
    _formattedDateString = [_dateformatter stringFromDate:[NSDate date]];
   _thetimelabel.stringValue =_formattedDateString;

    if (_thetimelabel.stringValue == _settimelabel.stringValue ) {

        if (_messageCheckBox.state == 1)
        {
            //show notification
            NSUserNotification *notification = [[NSUserNotification alloc] init];
            notification.title = @"FastAlarmClock: Alert!";
            notification.informativeText = _textfeild.stringValue;
            notification.soundName = NSUserNotificationDefaultSoundName;

            [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
        }

        //play the alarm
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Alarm" ofType:@"mp3"];
        NSURL* file = [[NSURL alloc] initFileURLWithPath:filePath];
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
        [audioPlayer prepareToPlay];

        [audioPlayer play];

    }


the error is at
if (_thetimelabel.stringValue == _settimelabel.stringValue ) {


they are both labels, they both end up matcing, this is an alarm clock, i don't understand why its not firing, can some one please explain? I do understand! its like if "lol" = "lol" do something. imm so confused
Posted

1 solution

By testing for the two strings being == you are actually asking whether the objects pointed to by the variables are the same object.

And I guess they're not.

You need to check if the string values of those objects are equal

if ([_thetimelabel.stringValue isEqualTostring:_settimelabel.stringValue] ) {


When you use constants

C++
NSString* myVar1 = @"foo";
NSString* myVar2 = @"foo";


then myVar1 == myVar2 is true - but onlyh because the compiler spots that the two constants are the same, and so references the same object. When you have two NSString* that have values, they (usually!) will point to completely different objects, and so are not equal.
 
Share this answer
 
Comments
[no name] 12-May-13 22:30pm    
thank you so much!!! xcode and their stilly logic
_Maxxx_ 12-May-13 22:43pm    
No worries. It's a common mistake for new OO programmers (this isn't an XCode issue - it's normal practice for many OO languages, including Objective C).

An NSString is an object. So, when you have two objects, how can you compare them for equality?

If it was, say, a BaddySprite object in a game, or a CustomerObject in a business application, it is quite clear that saying if(object1 == object2) is open to interpretation - do we have two instances of the same baddy (or customer) or a single instance? Are they equal if their name property's match?
It is one of those things that is so easy to do wrong that, even now you know, I bet you do it wrong again (I know I still do)!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900