简体   繁体   English

为什么我的if语句第一次不起作用? (Objective-C,Xcode,iPhone)

[英]Why does my if statement not work the first time? (Objective-C, Xcode, iPhone)

New to OB-C here. OB-C的新功能在这里。 Java programmer by trade. Java程序员按行业分类。

This is a sample from my view.m file. 这是我的view.m文件中的示例。 I have a display that takes some numbers from buttons. 我有一个从按钮上取一些数字的显示器。 If the display is showing 0, it should clear the 0 first before appending new numbers. 如果显示屏显示0,则应在附加新数字之前先清除0。

- (IBAction) btn1:(id) sender{[self numPress: (NSString *) @"1"];};
- (IBAction) btnClear:(id) sender{display.text = @"0";};

- (void) numPress : (NSString *) key {
    if (display.text ==  (NSString *) @"0")
        display.text = @"";
    display.text =  [display.text stringByAppendingString: key];
}

- (void) viewDidLoad {
    self.display.text = (NSString *) @"0";
}

When I run it as shown, the display sets to '0' then if I hit '1' the display changes to '01'. 如图所示运行它时,显示设置为“ 0”,然后如果我按“ 1”,则显示更改为“ 01”。 If I press Clear the display resets to '0', then I if hit '1' again, it changes to '1'. 如果我按清除,则显示将重置为“ 0”,如果我再次按“ 1”,则显示将变为“ 1”。 (the intended result). (预期结果)。 The first time I pressed '1' the 0 should have been removed. 我第一次按“ 1”时,应该删除0。

If I change the viewDidLoad to set the display.text to '2' and change the numPress if statement condition to '2' it works. 如果我将viewDidLoad更改为将display.text设置为“ 2”,并将numPress if语句条件更改为“ 2”,那么它将起作用。

- (IBAction) btn1:(id) sender{[self numPress: (NSString *) @"1"];};
- (IBAction) btnClear:(id) sender{display.text = @"0";};

- (void) numPress : (NSString *) key {
    if (display.text ==  (NSString *) @"2")
        display.text = @"";
    display.text =  [display.text stringByAppendingString: key];
}

- (void) viewDidLoad {
    self.display.text = (NSString *) @"2";
}

I build then run and the display says '2'. 我先构建然后运行,显示屏显示“ 2”。 I press '1' and the display changes to '1'. 我按“ 1”,显示变为“ 1”。 (clearing the leading '2' as it should). (应清除开头的“ 2”)。

Can someone help me here, why does '2' work, but '0' fails (but only the fist time), that boggles my mind? 有人可以在这里帮助我,为什么“ 2”有效,但“ 0”无效(仅第一时间),这使我感到困惑?

Use -isEqualToString: instead of comparing pointers (with == ). 使用-isEqualToString:而不是比较指针(使用== )。

if ([display.text isEqualToString:@"..."])
    /* ... */ ;

You'll find that you're doing your comparisons wrong 您会发现自己做的比较不正确

== when used with pointers compares the actual pointer values and not the actual objects. ==当与指针一起使用时,将比较实际的指针值而不是实际的对象。

You should rewrite it to this. 您应该将其重写为此。

- (IBAction) btn1:(id) sender {[self numPress:@"1"];}; // no need to cast, you have an NSString literal
- (IBAction) btnClear:(id) sender {display.text = @"0";};

- (void) numPress : (NSString *) key {
    if (display.text isEqualToString:@"0")
        display.text = @"";
    display.text =  [display.text stringByAppendingString: key];
}

- (void) viewDidLoad {
    self.display.text = @"0"; // again you have a literal
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM