运行应用程序时,Swift NSURL为零

时间:2016-03-17 11:33:14

标签: ios swift nsurl

当我运行应用程序Xcode告诉我 在展开Optional值时意外地发现了nil 在网址上但网址不是,有人可以帮忙吗?

这是代码

...
IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);

Socket s = new Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp);

String data = "Message From Server";
byte[] msg = Encoding.ASCII.GetBytes(data);
//Send to Client by UDP
s.SendTo(msg, endPoint);
...

2 个答案:

答案 0 :(得分:2)

您的路径字符串中可能有错误,请尝试以下操作:

let path = "http://api.wunderground.com/api/48675fd2f5485cff/conditions/geolookup/q/\(latitude),\(longitude).json"

原因是您在字符串中插入元组值\(latitude,longitude),这会增加额外的空间并使url字符串无效,因为空间不是百分比转义的。 相反,您必须使用逗号插入每个值:\(latitude),\(longitude)

答案 1 :(得分:0)

let path = "http://api.wunderground.com/api/48675fd2f5485cff/conditions/geolookup/q/\(latitude,longitude).json"

我认为你的意思是:

let path = "http://api.wunderground.com/api/48675fd2f5485cff/conditions/geolookup/q/\(latitude),\(longitude).json"