objective c,checking file exist at path

前端 未结 3 800
后悔当初
后悔当初 2020-12-23 13:41

I have created a screen using Objective-C in an iPhone project. In that, there are 2 buttons (say A and B). On the click of button A, one xml file will be created in a folde

相关标签:
3条回答
  • 2020-12-23 13:41

    try this:

    - (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;
    }
    
    0 讨论(0)
  • 2020-12-23 14:01

    Can you Please Check NSFileManager.

    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSString *pathForFile;
    
    if ([fileManager fileExistsAtPath:pathForFile]){ 
    
    }
    
    0 讨论(0)
  • 2020-12-23 14:08

    There have been similar questions on SO before:

    1. This answer is probably most appropriate for you

    Link to the NSFileManagerAPI

    0 讨论(0)
提交回复
热议问题