Swift调用OC之文件操作1

//***************************************************

//

//      >>>>>>>Swift调用OC之文件操作1<<<<<<<

//

//

// ****  本节内容 ****

//

// 1.应用的目录结构

// 2.访问文件

// 3.NSURLNSURLComponents

//

//

//***************************************************

 

import Foundation

 

//***************************************************

//

// >>>>1.应用的目录结构:

// 1.MyApp.app - 这就是应用的运行目录/(bundle),里面包含可执行文件和资源文件。AppStore购买应用后会安装到该目录

// 2.Documents - 用来存储用户产生的内容

// 3.Documents/Inbox - 用来访问被外部应用请求的当前应用的文件

// 4.Library - 用来存储非用户数据,比如配置文件.该目录对用户是不可见的,除了Caches子目录,其他都能通过iTunes备份

// 5.tmp - 用来存储那些下次重启不需要重新载入的临时信息,应用不运行时会自动清理.该目录内容也不会通过iTunes备份

//

//

// CommandLine是没有Bundle,Application才有,下面涉及Bundle的操作都需要放到APP里面才能运行正确,比如放到viewDidLoad()方法内

//***************************************************

 

//**************************************

//

//****        2.访问文件       *****

//

// >>可以使用StringNSURL来描述文件路径。官方推荐使用NSURL

// >>需要在Build Phases>>Target-Copy>>BundleResource里面添加了文件才能获取到文件路径

//**************************************

 

//使用String获取文件路径

if let filePath= NSBundle.mainBundle().pathForResource("3001",ofType: "jpg") {

    println("filePath=\(filePath)") //Users/..../data/Containers/Bundle/Application/../TEST1.app/3001.jpg

}

 

//获取当前应用的临时文件目录(tmp)

let tempDirectory101 = NSTemporaryDirectory()

 

//获取当前用户域下的目录

//根据NSSearchPathDirectory参数确定查找哪个目录:Page184

//.DocumentDirectory - Documents目录

//.ApplicationDirectory - Applications目录

//....

//根据NSSearchPathDomainMask的参数确定朝赵的文件系统域:Page185

//UserDomainMask- 用户域,指向当前用户的home目录(~) *这个最常用

//...

let directories101 = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) //Users/EvaZis-Pro/Library/Containers/com.apple.dt.playground.stub.OSX.C01------C0ADA375-9749-4FF6-9DE5-AA2025E518AE/Data/Documents"

 

 

//**************************************

//

//Page185

//

//       3.NSURLNSURLComponents

//

//

//**************************************

// URL结构

// 举例:http://blog.csdn.net/qzidane?viewmode=contents

// scheme   : http (还有httpsftpfiledata)

// host    : blog.csdn.net

//path      : /qzidane

// 查询语句  :  viewmode=contents

// 等等....

 

 

//***************************************

//

//**********      创建URL     ************

//

//***************************************

 

//方法1:指定scheme/host/path

let mutableURL201 = NSURL(scheme:"Http", host: "blog.csdn.net", path: "/qzidane")

println(mutableURL201!) //http://blog.csdn.net/qzidane

 

let emptyHostURL201 = NSURL(scheme:"file", host: nil, path: "/TEST1/C01")

println(emptyHostURL201!) //file:///TEST1/C01

let emptyHostURL202 = NSURL(scheme:"data", host: nil, path: "/TEST1/C01")

println(emptyHostURL202!) //data:///TEST1/C01

 

//方法2:指定基础URL和相对URL

let baseURL201 = NSURL(string:"file:///TEST1/")

let url201 = NSURL(string:"C01/XX.html", relativeToURL: baseURL201) //file:///TEST1/C01/XX.html

let absoluteURL201 = url201?.absoluteURL   // file:///TEST1/C01/XX.html

let absoluteURL202 = url201?.absoluteString //"file:///TEST1/C01/XX.html"

 

//注意对复杂的URL(比如说含有中文),需要进行UTF8编码转换,否则无法得到正确的URL

letcomplxString =  "http://www.abc.com/中文网址/hh.html"

let complxURL1 = NSURL(string: complxString// 这样得到的URLnil

let converString = complxString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) //http://www.abc.com/%E4%B8%AD%E6%96%87%E7%BD%91%E5%9D%80/hh.html

let complxURL2 = NSURL(string:converString!)

 

//方法3:使用fileURLWithPath指定文件或目录的路径来得到URL

//其中isDirectory参数为true是返回一个目录("/"),false则不带"/"

let path = NSURL.fileURLWithPath("/Hello"// Some file:///Hello

let pathDirectory = NSURL.fileURLWithPath("/HeLLO",isDirectory: true)! //file:///HeLLO/

 

//***************************************

//

//**********  获得URL指定部位  ************

//

//***************************************

let url1 = NSURL(string: "https://www.abc.com:99999/haha/ttt?name=qqq#atat")

url1?.host     // www.abc.com

url1?.scheme    // https

url1?.port     // 99999

url1?.query     // name=qqq

url1?.fragment  // atat

url1?.lastPathComponent // ttt

url1?.pathComponents   // "/", "haha","ttt"

url1?.path             ///haha/ttt

url1?.relativePath     // /haha/ttt

 

let url2 = NSURL(string: "http://zidane:1234@abc.com/Hello/HAHA/oxox.png")

url2?.user     // zidane

url2?.password  // 1234

url2?.pathExtension //png

 

//********************************

//

//**********  文件URL  ************

//

//********************************

//检查资源是否可达:

//在获得文件URL后,使用URLForResourcecheckResourceIsReachableAndReturnError,找到返回true,否则返回nil

let fileURL = NSBundle.mainBundle().URLForResource("3001", withExtension: "jpg")

let fileURLReachable = fileURL?.checkResourceIsReachableAndReturnError(nil)

 

//判断是否为文件URL

let webURL = NSURL(string: "http://www.17kqw.com/in/1234")

webURL?.fileURL

fileURL?.fileURL

 

//路径拼接(修改路径)

let modiURL = NSURL(string: "http://www.17kqw.com/hello")

//增加文件或目录

let URL1 = modiURL?.URLByAppendingPathComponent("HiH.html") //Some http://www.17kqw.com/hello/HiH.html

let URL2 = modiURL?.URLByAppendingPathComponent("DDD", isDirectory: true) //Some http://www.17kqw.com/hello/DDD/

//增加后缀

let URL3 = modiURL?.URLByAppendingPathExtension("png") //Some http://www.17kqw.com/hello.png

//删除最后一个路径/删除后缀

let URL4 = modiURL?.URLByDeletingLastPathComponent     //Some http://www.17kqw.com/

let URL5 = URL3?.URLByDeletingPathExtension           //Some http://www.17kqw.com/hello

 

//*****************************************

//

//********** NSURLComponents  ************

//

// 只支持IOS7.0及以上的API类,可与NSURL互相转换

//

//*****************************************

let component = NSURLComponents(string: "http://www.17kqw.com/hello")

 

//可以直接拼装和赋值:

component?.user = "Zidane"

component?.password = "Password1234"

component?.port = 8888

component?.query = "toKey=1111"

component?.scheme = "https"

component?.fragment = "XO"

component?.path = "/中文/hey"

 

//结果如下:

component?.URL  //https://Zidane:Password1234@www.17kqw.com:8888/%E4%B8%AD%E6%96%87/hey?toKey=1111#XO

component?.percentEncodedPath //"/%E4%B8%AD%E6%96%87/hey"

component?.path //"/中文/hey"

component?.percentEncodedPassword

//component?....还有很多方法


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值