Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First off you cannot write to anything in you mainBundle. For you to write to you plist you need to copy it to the documents directory. This is done like so:</p> <pre><code>- (void)createEditableCopyOfIfNeeded { // First, test for existence. BOOL success; NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"Template.plist"]; success = [fileManager fileExistsAtPath:writablePath]; if (success) return; // The writable file does not exist, so copy from the bundle to the appropriate location. NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Template.plist"]; success = [fileManager copyItemAtPath:defaultPath toPath:writablePath error:&amp;error]; if (!success) NSAssert1(0, @"Failed to create writable file with message '%@'.", [error localizedDescription]); } </code></pre> <p>So calling this function will check if the file exists in the documents directory. If it doesn't it copies the file to the documents directory. If the file exists it just returns. Next you just need to access the file to be able to read and write to it. To access you just need the path to the documents directory and to add your file name as a path component.</p> <pre><code>NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filePath = [docDir stringByAppendingPathComponent:@"Template.plist"]; </code></pre> <p>To get the data from the plist:</p> <pre><code>NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath]; </code></pre> <p>To write the file back to the documents directory.</p> <pre><code> [array writeToFile:filePath atomically: YES]; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload