Search code examples
iosobjective-cplistfile-read

How to read a file without extension in Objective C


I have a file stored in my iOS App Documents directory. It is a plist file but without .plist file extension. I want to read its content from code but i cant open the file without extension. Is there any way to add plist extension or convert the file into plist, or any way to read content of a file without extension.

This is how my code look like

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"sample.bookmarks"];
filePath = [filePath stringByAppendingPathExtension:@"plist"];
NSDictionary *dict=[NSDictionary dictionaryWithContentsOfFile:filePath];

Thanks in Advance

PS : If I add the .plist extension in finder(by some method) I can open the file and see the content. So I am sure about its type.


Solution

  • It should be possible to load the Plist file whether it has a .plist extension or not. Assuming the Plist filename is sample.bookmarks then the following should load the Plist. I am assuming the root element of the Plist is a dictionary because you state that the Plist can be opened if it has a .plist extension.

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"sample.bookmarks"];
    NSDictionary *dict=[NSDictionary dictionaryWithContentsOfFile:filePath];
    

    The only change is that I've removed the line that adds a plist extension to the filePath.

    The filePath = [filePath stringByAppendingPathExtension:@"plist"]; line will change the filename portion of the filePath to sample.bookmarks.plist. If you changed the extension to an empty string (filePath = [filePath stringByAppendingPathExtension:@""];) then the filename will be changed to sample.bookmarks. which is not the same as sample.bookmarks.