0
self.toDoItems = [[NSMutableArray alloc] init]; //initialize array
self.dataFileName = @"PropertyList";
self.filePath = [self.dataFileName stringByAppendingString:@".plist"];
self.rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                          NSUserDomainMask, YES) objectAtIndex:0];
self.filePath = [self.rootPath stringByAppendingPathComponent:self.filePath];
self.NSSRootNodeName = @"ToDoList"; //name of the root node in the property list

//    if (![[NSFileManager defaultManager] fileExistsAtPath: self.filePath]) {
//        self.filePath = [[NSBundle mainBundle] pathForResource:self.dataFileName   ofType:@"plist"];
//    }

 NSLog(@"File path: %@", self.filePath);

// Uncomment the following line to preserve selection between presentations.
self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem;

//read xml file and load to array
NSString *errorDesc = nil;
NSPropertyListFormat format;

NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:self.filePath];
if (plistXML==nil) {
    NSLog(@"Error reading plistXML");
}
NSDictionary *rootDictionary = (NSDictionary *)[NSPropertyListSerialization
                                                propertyListFromData:plistXML
                                                mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                                format:&format
                                                errorDescription:&errorDesc];
if (!rootDictionary) {
    NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
} 

the output is below. I am not sure why I get these errors below: My PropertyList.plist is sitting in my Documents folder inside my application. could someone point out what I am doing wrong.

File path: /Users/Computer/Library/Application Support/iPhone Simulator/7.1/Applications/78B5F9BA-8376-4001-ACA0-936D9B4D6342/Documents/PropertyList.plist

Error reading plistXML
Error reading plist: stream had too few bytes, format: 0
rmaddy
  • 314,917
  • 42
  • 532
  • 579
HaiNguyen
  • 312
  • 2
  • 10
  • try check: if ([[NSFileManager defaultManager] fileExistsAtPath: self.filePath]) before to read contentsAtPath: – stosha Apr 27 '14 at 14:42
  • If I un-comment those lines about application bundle. then it works for the read part, but I still have issue with write the changes back to the property file. my understanding is according to apple if it could not find the file from ~/documents folder it will try in the application bundle, so that makes senses. But how do I write my changes back? – HaiNguyen Apr 27 '14 at 14:52
  • @HaiNguyen: You cannot *write* to the application bundle, it is read-only. So even if you read the plist from the application bundle, you should write it to the Documents directory (or some other writable directory). – Martin R Apr 27 '14 at 15:26
  • @Martin R, That was what I heard too, but if that is the case how do I update the file so that next time my app will pickup new data? my property files is actually under a documents folder which is inside the application root, so I guess that also consider in application bundle? on the side note, anyone show me how to add code on the this mini markdown reply? – HaiNguyen Apr 27 '14 at 15:34
  • possible duplicate of [IOS: copy a file in documents folder](http://stackoverflow.com/questions/6545180/ios-copy-a-file-in-documents-folder) – Martin R Apr 27 '14 at 15:51
  • @HaiNguyen: On the first run of your application you have to copy the file in the app bundle (which is installed together with the app) to the Documents directory. The answer to the above "possible duplicate" shows how this is down. – Martin R Apr 27 '14 at 15:53
  • @Martin R, Could you show some code how to get document directory? I used something like this ` NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *result = [documentsDirectory stringByAppendingPathComponent: @"ProperList.plist"]; printf("filePathForState: %s\n", [result UTF8String]);` and it returns this ` /Users/Computer/Library/Application Support/iPhone Simulator/7.1/Applications/78B5F9BA-8376-4001-ACA0-936D9B4D6342/Documents/PropertyList.plist` – HaiNguyen Apr 28 '14 at 01:14
  • @HaiNguyen: Then `documentsDirectory` *is* the "Documents" directory. Where is the problem? – Martin R Apr 28 '14 at 05:07
  • I got it works finally. Thanks a lot guys! I wish I was 1/2 smart as you guys. Just today I came to realize that my app always reads the file from application bundle and so regardless where I write, I never see anything changes because the app always grab the old data file and display on UI. My guess the root of the whole problem is because I am new to MAC OS, I still don't know how to navigate to the documents directory ([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory...) to see what files I have there. I added code to copy file to documents dir and read/write from that path. – HaiNguyen Apr 29 '14 at 03:18

1 Answers1

0

FWIW, here is what worked for me:

NSFileManager *fileManager = [[NSFileManager alloc] init];
if ([fileManager fileExistsAtPath: [self filePathForState]])
{
    printf("found state file\n");
    NSData *data = [NSData dataWithContentsOfFile:[self filePathForState]];
    ...
}
else 
{
    printf("no state file found\n");
}
[fileManager release];

- (NSString *) filePathForState
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *result = [documentsDirectory stringByAppendingPathComponent: @"circles06"];
    printf("filePathForState: %s\n", [result UTF8String]);
    return result;
}

- (void) saveState
{
    NSData *state = [model stateData];
    [state writeToFile: [self filePathForState] atomically: NO];
}
Rudi Angela
  • 1,463
  • 1
  • 12
  • 20
  • What is the value for filePathForState look like? - Thanks – HaiNguyen Apr 27 '14 at 15:39
  • I used your code the way it is in the viewDidLoad method, replaced "circles06" with my file "PropertyList.plist" and I am getting this message below. Do you know what else I could try? [link](http://example.com) _italic_ **bold** `code` filePathForState: /Users/Computer/Library/Application Support/iPhone Simulator/7.1/Applications/78B5F9BA-8376-4001-ACA0-936D9B4D6342/Documents/PropertyList.plist no state file found[/code] – HaiNguyen Apr 28 '14 at 01:03
  • This indicates that you have not yet created a file. This code is for reading the file, not for creating it or writing to it. I have now added my method for writing to the file (verbatim from my code). – Rudi Angela Apr 29 '14 at 05:34