【问题标题】:invalid operands to binary expression ('NSString *' and 'NSString *')二进制表达式的无效操作数('NSString *' 和 'NSString *')
【发布时间】:2014-02-17 08:31:19
【问题描述】:

我有以下代码:

NSString  *String=TextField1.text + TextField2.text 

它给出了错误:-invalid operands to binary expression ('NSString *' and 'NSString *')

【问题讨论】:

  • 在objective-c '+' 运算符不连接字符串。使用 NSString 的工厂方法之一。
  • 另外,按照惯例,Objective-C 中的变量名通常以小写字母开头。 (例如textField1textField2)。

标签: ios iphone objective-c string int


【解决方案1】:

你不能这样做,因为 Objective-C 不使用 '+' 运算符进行连接。

这种方式应该可行:

NSString *concat = [NSString stringWithFormat: @"%@%@", TextField1.text, TextField2.text];

NSString *concat = [TextField1.text stringByAppendingString:TextField2.text];

希望这对你有用;)

【讨论】:

    【解决方案2】:

    你应该使用以下

    NSString *concat = [TextField1.text stringByAppendingString:TextField2.text];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-16
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      相关资源
      最近更新 更多