NSSearchPathForDirectoriesInDomains NSUserDomainMask
As On your Requirment No Source code and you showing the Exception so we have these path for wrting and reading. and share what you using ?
Documents
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
Library
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
cache directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;
if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO) {
[[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
}
For reading and wrting you can use the path.And for more see this Answer
aparna
Updated on June 16, 2022Comments
-
aparna almost 2 years
Using URLConnection I am downloading a file from a url and storing that data to file in documents directory. When I receive response, when the didReceiveResponse: method called, I create file and move to end of that file this way:
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:self.fileName]; NSLog(@"filepath is %@",filePath); [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil]; file = [[NSFileHandle fileHandleForUpdatingAtPath:filePath] retain]; [file seekToEndOfFile];
where File is object of NSFileHandle class. Later while receiving data, whenever the didReceiveData:(NSData *)data method called, I store the received data to the file this way:
[file seekToEndOfFile]; [file writeData:data];
Finally I close the file in connectionDidFinishLoading: method this way:
[file closeFile];
It was working when I build and run in my system. But when I copied to another system and tried to build I get this error :
Terminating app due to uncaught exception `'NSFileHandleOperationException'`, reason: '*** `-[NSConcreteFileHandle seekToEndOfFile]: No such file or directory'`
I refered http://www.techotopia.com/index.php/Working_with_Directories_on_iPhone_OS and What is NSSearchPathForDirectoriesInDomains? for knowing about NSSearchPathForDirectoriesInDomain function but I am still not clear.
Will
UserDomainMask
return the proper path depending on system and user in which the project exists. If so why I am getting such error?They say the path returned (stored in filepath) will be the path /Users//Library/Application Support/iPhone Simulator/User/Application//Documents where
is the name of the user currently logged into the Mac OS X system on which the simulator is running and is the unique ID of the app
Can anyone help me, to understand why I am getting such error.
-
aparna almost 11 yearsI understood how to use it. I have changed my question to include few code for explanation. My doubt was in the path returned.
-
Buntylm almost 11 years@aparna not too much clear please try my code lines clean code and then test
-
aparna almost 11 yearsk I will change the code and I will let you know if it works. thanks.
-
Blazej SLEBODA almost 9 yearsit's safer to use [... firstObject] instead [... objectAtIndex:0]; example: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachePath = [paths firstObject];
-
Subhojit Das over 8 yearsYou can get the file path by just calling this function while using sqlite3 *details; int result = sqlite3_open([[self filePath] UTF8String], &details); if(result != SQLITE_OK)