今天晚上最后一個(gè)例子,寫完休息娛樂一會(huì),
ObjectC,NSURL,統(tǒng)一資源定位器
。URL,統(tǒng)一資源定位器,可以定位網(wǎng)絡(luò)上的一個(gè)資源。
沒啥難的,還是對(duì)象、方法、API。和Java等語(yǔ)言沒有啥區(qū)別。
不親自一點(diǎn)點(diǎn)寫一遍,印象不深,今后進(jìn)一步深入學(xué)習(xí)iOS時(shí),會(huì)遇到很多問題,倒是再回頭看,也沒效率。
代碼
//// main.m// NSURLTest//// Created by fansunion on 15/12/2.// Copyright (c) 2015年 demo. All rights reserved.//#import<foundation foundation.h="">int main(int argc, const char * argv[]) { @autoreleasepool { NSString* str = @"http://blog.csdn.net/FansUnion"; NSURL* url = [NSURL URLWithString:str]; NSString* scheme =[url scheme]; NSString* host = [url host]; NSString* port = [url port]; NSString* path =[url path]; NSLog(@"scheme : %@",scheme); NSLog(@"host : %@",host); NSLog(@"port : %@",port); NSLog(@"path : %@",path); //使用URL對(duì)應(yīng)的資源來(lái)初始化NSString對(duì)象 NSString* content =[NSString stringWithContentsOfURL: url encoding:NSUTF8StringEncoding error:nil]; NSLog(@"%@",content); } return 0;}</foundation>
程序輸出
2015-12-02 21:07:54.545 NSURLTest[5619:393442] scheme : http
2015-12-02 21:07:54.546 NSURLTest[5619:393442] host : blog.csdn.net
2015-12-02 21:07:54.546 NSURLTest[5619:393442] port : (null)
2015-12-02 21:07:54.546 NSURLTest[5619:393442] path : /FansUnion
2015-12-02 21:08:08.555 NSURLTest[5619:393442]
更多html內(nèi)容,沒有展示出來(lái),太長(zhǎng)了,
電腦資料
《ObjectC,NSURL,統(tǒng)一資源定位器》(http://www.szmdbiao.com)。比較奇怪的是,端口號(hào)port為啥是null,難道不是80么?