xcode 4 - 简单代码不工作? [英] xcode 4 - Simple code not working?
问题描述
这是我的代码
this is my code
//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 ) {
他们都是标签,他们最终都是成熟的,这是一个闹钟,我不明白为什么它不解雇,有人可以解释一下吗?我明白了!就像lol=lol做某事一样。 imm so confused
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
推荐答案
通过测试两个字符串是==你实际上是在询问变量指向的对象是否是同一个对象。
我猜他们不是。
您需要检查这些对象的字符串值是否相等
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
NSString* myVar1 = @"foo";
NSString* myVar2 = @"foo";
然后myVar1 == myVar2为真 - 但只有因为编译器发现两个常量相同,因此引用相同的对象。当你有两个具有值的NSString *时,它们(通常是!)将指向完全不同的对象,因此不相等。
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.
这篇关于xcode 4 - 简单代码不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!