可以參考這個
新接手的項目與原來的項目在請求接口的業(yè)務區(qū)分上有差別脸甘,原負責項目的接口區(qū)分業(yè)務都是用接口中的某個參數(shù)暫且叫他busnessName區(qū)分的履肃,接收新項目后發(fā)現(xiàn)這個項目區(qū)分業(yè)務是用接口的path路徑區(qū)分的,本想著原來的網(wǎng)絡框架可拿過來直接使用呢扣蜻,結果出個這逆巍,此時NSUrl/NSUrlComponents的用武之地就到了
接口的組成:
<scheme name> : <hierarchical part> [ ? <query> ] [ # <fragment> ]
nsurl.png
NSUrl操作
let baseURL = NSURL(string: "http://example.com/v1/")
NSURL(string: "foo", relativeToURL: baseURL)
// http://example.com/v1/foo
NSURL(string:"foo?bar=baz", relativeToURL: baseURL)
// http://example.com/v1/foo?bar=baz
NSURL(string:"/foo", relativeToURL: baseURL)
// http://example.com/foo
NSURL(string:"foo/", relativeToURL: baseURL)
// http://example.com/v1/foo/
NSURL(string:"/foo/", relativeToURL: baseURL)
// http://example.com/foo/
NSURL(string:"http://example2.com/", relativeToURL: baseURL)
// http://example2.com/
NSUrlComponents屬性,可對照上圖
scheme
user
password
host
port
path
query
fragment
對于我的需求是換完整接口的路徑path,此處只需在合成完整鏈接時添加一步替換path即可
NSURLComponents *url = [NSURLComponents componentsWithString:@"http://3g.fang.com.cn/index/path/interFace?date=all&name=beijing"];
url.path = @"/haha/dudu/dede";
NSLog(@"=====%@",url);
NSLog(@"++++++++++%@",url.string);//獲取完整的替換后的鏈接
完成莽使。
同時也有對個屬性的編碼輸出
percentEncodedUser
percentEncodedPassword
percentEncodedHost
percentEncodedPath
percentEncodedQuery
percentEncodedFragment
暫時沒用到在這里記錄一下