2013-04-01 3 views
0

쓰기 위해 여러 객체를 인코딩했지만 파일 관리에 문제가 있습니다. 기존 파일을 열거 나 새 파일을 만들 수 없습니다.쓰기 용 파일을 만들 수 없습니다. - iOS

동일한 이름의 빈 파일을 만들거나 새 파일을 만들려고했습니다. 그것은 반응하지 않는 것 (또는 그 일은 전혀하지 않습니다).

편집 그래서 여기에 충고 한 내용 중 일부는 잘못된 액세스 충돌입니다.

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    filePath = [documentsDirectory stringByAppendingPathComponent:@"/bucketListData.dat"]; 

그리고 여기에 수정 된 방법이다 : 나는 경로를 얻을 경우 여기

입니다.

-(void) writeFile 
{ 
    NSData *encodedObject; 
    encodedObject = [NSKeyedArchiver archivedDataWithRootObject: bucketItems]; 
    NSFileHandle *file; 
    file = [NSFileHandle fileHandleForReadingAtPath:filePath]; 

    if(file == nil) 
    { 
     NSLog(@"Failed to open a file handle for writing. Attempting to create a file."); 
     [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil]; 
     file = [NSFileHandle fileHandleForReadingAtPath:filePath]; 
     if(file == nil) 
     { 
      NSLog(@"Unable to create new file."); 
     } 
    } 

    [file writeData: encodedObject]; 
    [file closeFile]; 
} 
+0

언어 자체에 대한 질문에만 [tag : objective-c] 태그를 사용하십시오. 감사! – Undo

+0

원본을 편집하여 변경 내용을 표현합니다. –

답변

4

당신은 루트에 쓸 수있는 권한이 없습니다, 당신은 단지 당신은 읽기 전용 처리 만드는, 을 샌드 박스에 액세스 할 수 있습니다. 다음과 같이 시도하십시오.

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *filePath = [docDir stringByAppendingPathComponent:@"bucketListData.dat"]; 
NSFileHandle *file = [NSFileHandle fileHandleForWritingAtPath:filePath]; 

if (!file) { 
    NSLog(@"Attempting to create the file"); 
    if (![[NSFileManager defaultManager] createFileAtPath:filePath 
               contents:nil 
               attributes:nil]) { 
     NSLog(@"Failed to create file"); 
    } 
    else { 
     file = [NSFileHandle fileHandleForWritingAtPath:filePath]; 
    } 
} 

NSLog(@"File handle: %@", file); 
+0

은 트릭을 수행하지 않은 것 같습니다. 파일은 여전히 ​​createFileAtPath를 호출 한 후에 null입니다. –

+0

@ScubaSteve 새 프로젝트에서 저에게 적합한 코드가 더 추가되었습니다. 다시 시도하십시오. [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex : 0] 인덱스 객체가 – Pascal

+0

필자는 .. 그것이 첫 번째 줄에 충돌 –

1

파일 시스템의 루트에서 파일을 열려고하지만 iOS에서 해당 파일에 액세스 할 수 없습니다. 대신 문서 디렉토리에 파일을 만듭니다

NSData* encodedObject = [NSKeyedArchiver archivedDataWithRootObject: bucketItems]; 

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
NSString* path = [basePath stringByAppendingPathComponent:@"bucketListData.dat"]; 
NSFileHandle* file = [NSFileHandle fileHandleForReadingAtPath:path]; 
1

문서 디렉토리에 파일을 작성해야합니다. 그리고 stringByAppendingPathComponent를 사용하여 전체 경로를 만듭니다. 이게 도움이되기를 바랍니다.

-(void) writeFile 
{ 
    NSData *encodedObject; 
    encodedObject = [NSKeyedArchiver archivedDataWithRootObject: bucketItems]; 
    NSFileHandle *file; 

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

    if(paths && [paths count]>0){ 
     documentsDirectory=[paths objectAtIndex:0]; 

     file = [NSFileHandle fileHandleForReadingAtPath:[documentsDirectory stringByAppendingPathComponent:@"/bucketListData.dat"]]; 

     if(file == nil) 
     { 
      NSLog(@"Failed to open a file handle for writing. Attempting to create a file."); 
      [[NSFileManager defaultManager] createFileAtPath:[documentsDirectory stringByAppendingPathComponent:@"/bucketListData.dat"] contents:nil attributes:nil]; 
      file = [NSFileHandle fileHandleForReadingAtPath:[documentsDirectory stringByAppendingPathComponent:@"/bucketListData.dat"]]; 
      if(file == nil) 
      { 
       NSLog(@"Unable to create new file."); 
      } 
     } 

     [file writeData: encodedObject]; 
     [file closeFile]; 
    } 
} 
관련 문제