Skip to content

Instantly share code, notes, and snippets.

@fra3il

fra3il/0.m Secret

Last active February 12, 2016 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fra3il/08463c50ffbd22fe1eb6 to your computer and use it in GitHub Desktop.
Save fra3il/08463c50ffbd22fe1eb6 to your computer and use it in GitHub Desktop.
[WP/159] mainBundle, Documents
// mainBundle 의 파일 목록 가져오기
- (void)mainBundleList {
NSString *path = [[NSBundle mainBundle] bundlePath];
NSArray *list = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSLog(@"%@", list);
// .jpg 로 끝나는 파일만 보여준다
NSArray *onlyJPG = [list filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF ENDSWITH '.jpg'"]];
NSLog(@"%@", onlyJPG);
}
// Documents 의 파일 목록 가져오기
- (void)documentsList {
NSString *path = [NSString stringWithFormat:@"%@/Documents/", NSHomeDirectory()];
NSArray *list = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSLog(@"%@", list);
}
// mainBundle 의 파일을 Documents 로 복사하기
- (void)copyFile {
// documents 폴더로 복사할 파일
NSArray *fileList = [NSArray arrayWithObjects:@"IMG_0657.MOV", @"maltese.jpg", nil];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
for (NSInteger i = 0; i < [fileList count]; i++) {
NSString *fileName = [fileList objectAtIndex:i];
NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
NSString *toPath = [documentsDirectory stringByAppendingPathComponent:fileName];
// documents 폴더에 파일이 존재하지 않으면 복사
if (![fileManager fileExistsAtPath:toPath]) {
if ([fileManager copyItemAtPath:filePath toPath:toPath error:nil]) {
NSLog(@"1");
} else {
NSLog(@"0");
}
}
}
}
// 특정 디렉토리 아래의 서브 디렉토리를 확인하는 방법
- (NSString *)findSubDirectory {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"image"];;
NSArray *list = [fileManager subpathsOfDirectoryAtPath:path error:nil];
for (NSString *string in list) {
BOOL isDir = NO;
[fileManager fileExistsAtPath:[path stringByAppendingPathComponent:string] isDirectory:&isDir];
if (isDir) {
NSLog(@"%@", string);
}
}
return nil;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment