Search code examples
iphoneiosplistnsfilemanager

Check if plist file does not exist


I know how to check if a plist file does exist...

if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
 //..etc
}

I would like to know if there is a way to check if a plist file does not exist?

I am having some issues trying to use something from my plist when it's not present, as I create the plist later. So I would like to check if the plist is not present in the directory. I will pass some default values to the objects I am going to use. That way I will not get an error; the if statement where I am comparing these values will not throw a fit, and later will get populated with the correct data.


Solution

  • If the code shown checks that the file does exist, you remove the ! to check that it doesn't:

    if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
    

    Or, you can use:

    if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath] != 0) {
    

    since the code returns 0 when the file does exist. You might want to check errno or an equivalent to see if it is a permissions problems versus a non-existent file or directory.