Note 12 沙盒与应用程序的结构,如何取资源路径

应该了解:

  1. ipa包结构
  2. 应用程序结构
  3. 应用程序沙盒

沙盒结构

  1. mainBundle //只读
  1. Documents //永久保存用户数据
  2. Library - Caches//临时数据
  1. 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 :告诉使用者 这个函数可能会发生异常

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,314评论 30 472
  • 218.241.181.202 wxhl60 123456 192.168.10.253 wxhl66 wxhl6...
    CYC666阅读 5,300评论 0 6
  • 循环引用:http://ios.jobbole.com/82077/类别的作用功能:1.扩充现有类的功能2.对现有...
    得一切从简阅读 3,465评论 0 1
  • 最近一朋友正准备跳槽,就从各处搜索整理一些基础,便于朋友复习,也便于自己复习查看. 1. 回答person的ret...
    smile丽语阅读 5,779评论 0 7
  • OC的理解与特性 OC作为一门面向对象的语言,自然具有面向对象的语言特性:封装、继承、多态。它既具有静态语言的特性...
    小楼昨夜有风雨阅读 3,635评论 0 0