Nice programing

목표 c, 경로에 파일이 있는지 확인

nicepro 2020. 12. 28. 22:32
반응형

목표 c, 경로에 파일이 있는지 확인


iPhone 프로젝트에서 Objective-C를 사용하여 화면을 만들었습니다. 거기에는 2 개의 버튼이 있습니다 (예 : A와 B). 버튼 A를 클릭하면 하나의 xml 파일이 폴더 (예 : INBOX)에 생성됩니다.

내 문제는 INBOX 폴더에 파일이없는 경우에만 파일을 생성해야한다는 것입니다. 어떻게 할 수 있습니까? 아무도 나에게 구문을 말할 수 있습니까?


NSFileManager를 확인하십시오.

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *pathForFile;

if ([fileManager fileExistsAtPath:pathForFile]){ 

}

이 시도:

- (BOOL)checkAndCopy {
    NSError **error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"yourFile.xml"]; 

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath: path])
    {
        // this copy an existing xml (empty?) file from main bundle:
        // try else to create a new file

        NSString *bundle =  [[ NSBundle mainBundle] pathForResource:@"yourFile" ofType:@"xml"];
        [fileManager copyItemAtPath:bundle toPath:path error:error];
        return YES;
    }
    return NO;
}

이전에도 비슷한 질문이있었습니다.

  1. 답변은 아마도 당신에게 가장 적절할 것입니다.

NSFileManagerAPI에 연결

참조 URL : https://stackoverflow.com/questions/5075416/objective-c-checking-file-exist-at-path

반응형