我已将我的XCode更新为6.3.2并且我正在向URL添加参数但是“+”运算符不起作用并且给我和下面的错误
这是我在其中添加strEmail
(NSString对象)作为参数的URL。
var urlstring = "http://example.com/api/api.php?action=getReceivedMessages&ReceiverEmail=" + strEmail
这是我得到的错误。
"Binary Operator '+' cannot be applied to operands of type 'String' and 'NSString' "
答案 0 :(得分:4)
您可以使用字符串插值
var urlstring = "http://example.com/api/api.php?action=getReceivedMessages&ReceiverEmail=\(strEmail)"
或将strEmail强制转换为String
var urlstring = "http://example.com/api/api.php?action=getReceivedMessages&ReceiverEmail=" + (strEmail as String)
答案 1 :(得分:1)
从Swift 1.2开始,NSString
到String
的隐式转换已被删除:
已删除从桥接的Objective-C类(NSString / NSArray / NSDictionary)到其对应的Swift值类型(String / Array / Dictionary)的隐式转换,使Swift类型系统更简单,更可预测。
您必须使用String
{<1}}将其明确转换为as
:
"...iverEmail=" + (strEmail as String)
答案 2 :(得分:0)
您需要将NSString
转换为String
类型。有不同的方法,见下文:
var urlstring = "http://example.com/api/api.php?action=getReceivedMessages&ReceiverEmail=" + String(strEmail)
或
var urlstring = "http://example.com/api/api.php?action=getReceivedMessages&ReceiverEmail="
+ (strEmail as String)
或
var urlstring = "http://example.com/api/api.php?action=getReceivedMessages&ReceiverEmail=\(strEmail)"