Swift 1.2添加参数到URL不起作用。早期工作完美

时间:2015-05-28 07:13:09

标签: swift url nsstring

我已将我的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' "

3 个答案:

答案 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开始,NSStringString的隐式转换已被删除:

来自the release notes

  

已删除从桥接的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)"