본문 바로가기

프로그래밍/iOS,macOS

파일 가져오기

Sandbox내의 디렉토리


ENUM

NSDocumentDirectory

app_root/Documents


NSCachesDirectory

app_root/Library/Caches



메쏘드

NSString * NSTemporaryDirectory()

app_root/tmp



각 path는 아래와 같이 얻을 수 있다.

NSArray *paths=NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);

NSString *dir=[paths objectAtIndex:0];

NSString *file=[dir stringByAppendingPathComponent:@"file_name.ext"];



tmp폴더 같은 경우 메쏘드 이므로 조금더 간단하다.

NSString *file=[NSTemporaryDirectory() stringByAppedingPathComponent:@"file_name.ext"];




NSData

파일 or URL로 부터 데이터 오브젝트 생성

NSData *myData=[NSData dataWithContentsOfFile:thePath];

NSData *myData=[NSData dataWithContentsOfURL:theURL];


UIImage와 같이 NSData객체를 지원하는 경우 해당 객체 생성이 가능하다

UIImage *image = [UIImage imageWithData:myData];


c 함수로 unsigned char [] 로부터 NSData를 생성하려면

NSData *myData=[NSData dataWithBytes:(const void *)char_array length:array_size];



NSFileManager

NSFileManager *manager = [NSFileManager defaultManager];

[manager createDirectoryAtPath:폴더 withIntermediateDirectories:YES attributes:nil error:nil];


파일이 존재하는지 확인

[manager fileExistAtPath:파일path]


NSData 오브젝트 파일에 쓰기

[manager createFileAtPath:파일path contents:데이터 attributes:nil];


'프로그래밍 > iOS,macOS' 카테고리의 다른 글

오토레이아웃 이것저것 / Constraint / UITextView  (0) 2016.10.25
커스텀객체와 인터페이스빌더  (0) 2016.10.23
스토리보드 / XIB  (0) 2016.10.22
싱글톤  (0) 2016.10.19
코어 데이터 정리  (0) 2016.10.18
슬라이드메뉴  (0) 2016.10.16
UITableView / UICollectionView  (0) 2016.10.16
뷰 에니메이션  (0) 2016.10.13
비동기 처리 ( GCD , NSOperation )  (0) 2016.10.13
[objective-c] 블록구문  (0) 2016.10.13