Search code examples
iosnsfilemanager

Find the given file path is valid in ios?


I am trying to create a class which has a method that receives a file path(NSString) as parameter.

I need to check the given file path is valid if it is valid I will do some writing operation on disk otherwise I ll return error.

So is it any best way to find out the given path is valid one?


Solution

  • There is no need to check first if path is valid. Just go ahead and try to create it and then check for success.

    createFileAtPath:contents:attributes: method of NSFileManager returns a BOOL that returns YES if the file already exists or did succeed in creating it.

    I suggest using this method and check the returned result. If it returns NO then return error.