应该了解:
- ipa包结构
- 应用程序结构
- 应用程序沙盒
沙盒结构
- mainBundle //只读
- Documents //永久保存用户数据
- Library - Caches//临时数据
- tmp //临时数据
如何取得路径
mainBundle:又称资源文件夹,存放可执行程序,图片,界面等资源
//应用程序目录(沙盒目录)
let path = NSHomeDirectory()
print(path)
//1. Documents
let docPath = path + "/Documents"
print(docPath)
//2. Documents
let docPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
print(docPaths.first!)
let cachesPaths = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)
print(cachesPaths.first!)
let tmpPath = docPath + "/tmp"
print(tmpPath)
//mainBundle路径
let mainPath = NSBundle.mainBundle().bundlePath
print(mainPath)
//app资源路径
let resourcePath = NSBundle.mainBundle().resourcePath
print(resourcePath)
//Info.plist
let infoPath = mainPath + "/Info.plist"
print(infoPath)
let htmlPath = NSBundle.mainBundle().pathForResource("Java教程新目录", ofType: "html")
print(htmlPath)
//URL: NSURL/URL
let urlPath = NSURL(fileURLWithPath: htmlPath!)
print(urlPath)
let resURL = NSBundle.mainBundle().URLForResource("Java教程新目录", withExtension: "html")
print(resURL)
将path转化成url
let urlPath = NSURL(fileURLWithPath: Path!)
import
import Foundation //基础框架
import 后面加框架名
framework & import
异常处理
do {
try
} catch{
}
大部分系统的函数抛出的都是NSError类型的异常
swift需要明确表明可能出错的语句
throws :告诉使用者 这个函数可能会发生异常