沙盒目录下创建文件夹


查看沙盒方法:

前往 -->  (按住option键) 资源库 --> Application Support --> iPhone Simulator --> ...

在沙盒目录下创建文夹,可以用

- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error 方法,

为了搞清楚这个withIntermediateDirectories:什么意思,专写了一Demo:

//  ViewController.m

#import "ViewController.h"

@interface ViewController ()
{
    NSString *_directoryPath;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *yesButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [yesButton setFrame:CGRectMake(20, 20, 280, 30)];
    [yesButton setTitle:@"withIntermediateDirectories:YES" forState:UIControlStateNormal];
    [yesButton addTarget:self action:@selector(yesButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:yesButton];
    
    UIButton *noButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [noButton setFrame:CGRectMake(20, 80, 280, 30)];
    [noButton setTitle:@"withIntermediateDirectories:NO" forState:UIControlStateNormal];
    [noButton addTarget:self action:@selector(noButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:noButton];
    
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    _directoryPath = [[documentPath stringByAppendingPathComponent:@"1/2"] copy];
}

- (void)yesButtonClick:(id)sender
{
    if (![[NSFileManager defaultManager] fileExistsAtPath:_directoryPath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:_directoryPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

- (void)noButtonClick:(id)sender
{
    if (![[NSFileManager defaultManager] fileExistsAtPath:_directoryPath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:_directoryPath withIntermediateDirectories:NO attributes:nil error:nil];
    }
}

- (void)dealloc
{
    [_directoryPath release], _directoryPath = nil;
    [super dealloc];
}

@end




发现点击yesButton时会创建文件夹成功,但点击noButton发现创建文件夹不成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值