CoderWang CoderWang - 4 months ago 31
iOS Question

fileExistsAtPath:filePath method of NSFileManager keep returning NO


  1. Menus.plist file in bundle;

  2. copy the file to URL_DocDir_MenusPlist;

    (#define URL_DocDir_MenusPlist [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject URLByAppendingPathComponent:@"Menus.plist"])

  3. go to the Document Directory and the plist exists, but

    [[NSFileManager defaultManager] fileExistsAtPath: URL_DocDir_MenusPlist.absoluteString]


    method keep returning NO.



PS. in device and in simulator.

thanks in advance :)

Answer

The issue is the property absoluteString with returns the full URL including in the scheme (file://).

The correct property for fileExistsAtPath is path

URL_DocDir_MenusPlist.path

Side notes:

  1. I'd recommend checkResourceIsReachableAndReturnError of NSURL instead of fileExistsAtPath

  2. Please use the Cocoa naming convention:

    • Variable names starting with a lowercase letter and camel case rather than underscore separation.
    • Constants or macro names with the entire name in uppercase letters and underscore separation.
Comments