SwiftCommon之Path路径
2015-07-29 10:53
435 查看
概述
获取路径也是用的非常多的,IOS的沙盒机制保证每一个App都有自己的数据目录。SCPath
import Foundation /** * 路径(Path)类 */ public class SCPath: NSObject{ /** 获取程序的主目录,包含Document、Library、Temp三个目录 */ public class func getHomePath()->String{ return NSHomeDirectory() } /** 获取Document目录路径 :returns: 返回当前App的Document路径 */ public class func getDocumentPath()->String{ return NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String } /// 获取Document目录路径(实现方式更简单) public class func getDocumentPathSimaple()->String{ return "~".stringByAppendingPathComponent("Documents").stringByStandardizingPath } /** 获取Cache目录路径 :returns: 返回当前App的Cache路径 */ public class func getCachePath()->String{ return NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String } /** 获取Temp目录路径 :returns: 返回当前App的Temp路径 */ public class func getTempPath()->String{ return NSTemporaryDirectory() } /** 获取APP Bundle包中的资源路径 :param: fileName 资源名(不带扩展名) :param: fileType 资源的文件类型(不带点.) :returns: 返回指定资源名在当前App Bundle中的完整路径 */ public class func getBundleResourcePath(#fileName:String,fileType:String)->String{ return NSBundle.mainBundle().pathForResource(fileName, ofType: fileType)! } /** 检测指定文件路径或者目录路径是否存在 :param: path 需要检测的文件(目录)路径 :returns: 如果路径存在返回true,否则返回false */ public class func isExists(#path:String)->Bool{ return NSFileManager.defaultManager().fileExistsAtPath(path) } /** 获取指定目录下面包含的文件路径数组 :param: directoryPath 需要获取的目录的路径 :returns: 返回包含所有的文件路径数组 */ public class func getAllFilesArray(#directoryPath:String)->NSArray{ let allFiles:NSArray = NSFileManager.defaultManager().contentsOfDirectoryAtPath(directoryPath , error: nil)! return allFiles } }
Git地址
http://devonios.com/go/swiftcommontips:
本文由wp2blog导入,原文链接:http://devonios.com/scpath.html
相关文章推荐
- SwiftCommon之Dictionary字典
- SwiftCommon之Device设备信息
- SwiftCommon之日期Date
- SwiftCommon之Cookie处理
- Swift:subscript
- OC和swift互相调用。
- swift 之函数式编程(一)
- Swift入门(一)——基本语法
- swift2 运算符函数
- iOS编程——Swift实现常见的递归算法(爬楼梯)2
- 基于swiftype应用于Hexo-Yilia-主题的站内搜索引擎
- Swift学习笔记系列——(12)继承
- T系列T-Swift0001
- Swift之贪婪的UIButton
- Swift之贪婪的UIButton
- 如何判断消息提醒是否开启
- iOS编程——Swift实现一个3D圆型旋转菜单
- swift 之 如何在函数中把闭包作为参数 进行回调
- 如何在OC中使用Swift如何在Swift中使用OC
- 自学Swift-斯坦福笔记整理(九) -- 滚动视图 闭包 多线程