【问题标题】:Why is this code not working in Xcode 4.5 while it use to work on Xcode 4.4为什么这段代码不能在 Xcode 4.5 中工作,而它曾经在 Xcode 4.4 上工作
【发布时间】:2012-09-28 04:12:01
【问题描述】:

为什么这段代码不能在 Xcode 4.5(使用 ipad 6.0 模拟器)上运行,而它曾经在 Xcode 4.4 上运行。(使用 ipad 模拟器 5.1)

- (IBAction)capitalDButtonTwo:(id)sender {
    if ([capitalDResultLabelTwo text] == @"+") {
        [capitalDResultLabelTwo setText:@"0"];
    } else {
        [capitalDResultLabelTwo setText:@"+"];
    }
}

这是一个按钮,它在第一次按下时将同一视图中标签中的文本设置为“+”,然后将文本设置为“0”,之后每次按下时设置为“+”。我想知道一个版本与另一个版本有什么不同,以至于这个简单的代码不起作用

【问题讨论】:

  • 我正在为sender 部分工作except。我正在使用sender.view,上面写着Property 'view' not found on object type 'id'。仍在尝试找出原因。
  • @AnnaFortuna 您需要将id 转换为您知道的实际类型,例如((UILabel *)id).text = @"Hello";。当你说你的工作正常时,我很困惑;你的意思是你使用这个完全相同的代码片段?如果是这样,您需要在下面查看我的答案。

标签: xcode4.5


【解决方案1】:

它不应该在任何一个 Xcode 版本中工作。您没有正确比较字符串:

语句[capitalDResultLabelTwo text] == @"+" 测试两个NSString 对象是否是完全相同的对象。我敢肯定,您的意图是测试标签的 content 是否与"+" 相同,因此您需要使用[NSString isEqualToString:]

- (IBAction)capitalDButtonTwo:(id)sender {
    if ([[capitalDResultLabelTwo text] isEqualToString:@"+"]) {
        [capitalDResultLabelTwo setText:@"0"];
    } else {
        [capitalDResultLabelTwo setText:@"+"];
    }
}

【讨论】:

  • 好吧,它似乎在 xcode 4.4(模拟器 5.1)中表现得如预期,但是是的,你是对的 #trojanfoe;我已经习惯使用 python 中的 (==) 了。我对编程很陌生(可能很明显)。我会尝试你的建议,不过谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-10
  • 1970-01-01
  • 2017-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多