objective c,checking file exist at path

前端 未结 3 801
后悔当初
后悔当初 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条回答
  •  Happy的楠姐
    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;
    }
    

提交回复
热议问题