Not able to write to Plist

前端 未结 2 985
孤独总比滥情好
孤独总比滥情好 2020-12-21 23:02

I have a very basic question about how to write to a plist. I added a \"Here.plist\" file to my resources folder. The following is my code:

NSString *path =          


        
相关标签:
2条回答
  • 2020-12-21 23:33

    I suspect your problem is due to the fact that you can't write to the application bundle, only to your application's document store.

    To obtain a path to the document store, use:

    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docStorePath = [searchPaths objectAtIndex:0];
    
    NSString *filePath = [docStorePath stringByAppendingPathComponent:@"XXXXX"];
    
    0 讨论(0)
  • 2020-12-21 23:45

    You can't write into the bundle. Try writing into the documents directory. For example:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSArray *array = [[NSArray alloc]initWithObjects:@"First", @"Second", @"Third", nil];
    [array writeToFile:[documentsDirectory stringByAppendingPathComponent:@"Here.plist" atomically:YES];
    
    0 讨论(0)
提交回复
热议问题