iphone开发返回Documents Tmp 应用程序资源文件夹路径

大伙都知道iphone开发下要经常返回文件夹的路径,但是苹果提供的api返回不同的文件夹又不一样,我在这里统一定义下接口,

以后无论是返回Documents还是Tmp还是应用程序资源文件夹路径都只要直接调用:

[self dataFilePath:@"data.plist" ofType:kDocuments]

下面是源代码:


#define kDocuments 1  /*返回应用程序沙盒下documents文件夹里的文件路径*/
#define kTmp 2        /*返回应用程序沙盒下Tmp文件夹里的文件路径*/
#define kAPP 3        /*返回应用程序沙盒下app文件夹里的文件路径*/
- (NSString *)dataFilePath:(NSString *)file ofType:(int)kType{
    NSString *pathFile = nil;
    switch (kType) {
        case kDocuments:
        {
            // NSDocumentDirectory代表查找Documents路径,NSUserDomainMask代表在应用程序沙盒下找
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            // ios下Documents文件夹只有一个
            NSString *documentsDirectory = [paths objectAtIndex:0];
            pathFile = [documentsDirectory stringByAppendingPathComponent:file];
            break;
        }
        case kTmp:
        {
            NSString *str = NSTemporaryDirectory();
            pathFile = [str stringByAppendingPathComponent:file];
            break;
        }
        case kAPP:
        {
            // 获得文件名
            NSString *str =[file stringByDeletingPathExtension];
            // 获得文件扩展路径
            NSString *str2 = [file pathExtension];
            pathFile = [[NSBundle mainBundle] pathForResource:str ofType:str2];
            break;
        }
        default:
            break;
    }
    return pathFile;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值