パスの取得

Documents

let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let documentsPath = NSHomeDirectory() + "/Documents"

Library

let path = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)[0]
let libraryPath = NSHomeDirectory() + "/Library"

Application Support

let path = NSSearchPathForDirectoriesInDomains(.ApplicationSupportDirectory, .UserDomainMask, true)[0]
let applicationSupportPath = NSHomeDirectory() + "/Library/Application Support"

Caches

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
let path = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0]
let cachesPath = NSHomeDirectory() + "/Library/Caches"

tmp

NSString *path = NSTemporaryDirectory();
let path = NSTemporaryDirectory()

iOS /tmpと/Cachesディレクトリの違いと運用方法 - Qiita
https://qiita.com/yusuga/items/8c057a67e0ac021c54bc
iOSアプリのファイル保存について - Qiita
https://qiita.com/nnsnodnb/items/13642c4a8d55641f893e

パスの連結

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"hello.txt"];
NSLog(@"filePath=%@", filePath);

存在

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dirPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"NewDir"];

if (![[NSFileManager defaultManager] fileExistsAtPath:dirPath]) {
    NSLog(@"file don't exist.");
}

フォルダの作成

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dirPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"NewDir"];

NSError *error = nil;
if(![[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error]) {
    NSLog(@"Error occurred when directory was making! [%@]", [error localizedDescription]);
}

移動

[[NSFileManager defaultManager] moveItemAtPath:filePath toPath:afterPath error: &error];

コピー

[[NSFileManager defaultManager] copyItemAtPath:filePath toPath:afterPath error: &error];

削除

[[NSFileManager defaultManager] removeItemAtPath:path error:nil];

内容の取得

NSError *error = nil;
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"hello" ofType:@"txt"];
NSString *document = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
NSLog(@"document=%@", document);
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.