您的位置:首页 > 移动开发 > Swift

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/swiftcommon

tips:

本文由wp2blog导入,原文链接:http://devonios.com/scpath.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: