我有形式如何將一個類似「?x = 123」的東西添加到NSURL中?
"http://abc.def.com:1234/stuff/"
的NSURL,我想追加到它,因此最終的網址是這樣
"http://abc.def.com:1234/stuff/?x=123".
但如果我這樣做
url = [NSURL URLWithString:@"http://abc.def.com:1234/stuff/?"];
url = [url URLByAppendingPathComponent:@"x=123"];
然後結果是
http://abc.def.com:1234/stuff/x=123?
(如果使用URLByAppendingPathExtension,則結果相同)。
但如果我這樣做
url = [NSURL URLWithString:@"http://abc.def.com:1234/stuff/?"];
url = [url URLByAppendingPathComponent:@"x=123"];
那麼結果是
http://abc.def.com:1234/stuff/%3Fx=123
(也是同樣的結果,如果使用URLByAppendingPathExtension)。
這兩者都不是我所追求的。我如何獲得「http://abc.def.com:1234/stuff/?x=123」的最終結果?
當我嘗試上面顯示的方法我的網址不是正確的它出現像「?x = 1&y = 2&z = 3 http://foo.com/bar/baz」倒轉請提供一些解決方案如果你有這個。 謝謝 – 2015-11-20 07:46:13
@jalakpatel'url.description'返回'?x = 1&y = 2&z = 3 - http:// foo.com/bar/baz',所以'NSLog(@「URL:%@」,url)'將把'「URL:?x = 1&y = 2&z = 3 - http://foo.com/bar/baz」'放入調試器。如果你想顯示完整的URL,使用'url.absoluteString'。例如,'NSLog(@「URL:%@」,url.absoluteString)'會將':URL:http://foo.com/bar/baz?x=1&y=2&z=3「'放入調試器。 – 2015-11-20 10:42:17
@jalakpatel注意:'url.description'僅用於顯示。將'url'傳遞給任何需要URL的方法將會到達正確的位置。 – 2015-11-20 10:47:08