iOS : I can't write my file

后端 未结 2 1477
天涯浪人
天涯浪人 2021-01-28 12:57

I want to write a file on my iPhone app. My file is in my project\'s folder, but when I try to reach it :

NSString *myFile = [[NSBundle mainBundle] pathForResour         


        
2条回答
  •  一整个雨季
    2021-01-28 13:46

    You ask:

    Why?

    It's that path because that's where the file is stored.

    On the device it will be different.

    Note that you can't write a file to that folder anyway. You should perhaps instead write to your app's documents folder:

    //Get the documents folder
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    
    //Get the final path of your file.
    NSString *finalPath = @"myfile.txt"];    
    //And actually write that file.
    [[NSFileManager defaultManager] createFileAtPath:finalPath contents:/*fileData as NSData*/ attributes:nil];
    

提交回复
热议问题