This has become very frustrating over the past day-and-a-bit-too-much: why doesn't this work?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.plist",kAllScoresUnorderedArray]];
NSMutableDictionary *test = [[NSMutableDictionary alloc] init];
[test setObject:#"bob" forKey:#"tmo"];
[test writeToFile:path atomically: YES];
I have used it before, and it worked perfectly: is there something I am forgetting to add in my info.plist? That is the only thing I can think of.
Thank you for helping!
EDIT:
Maybe it has something to do with Xcode 4.5 and iOS6? It worked in 4.3/iOS5
Have you added the .plist file to your project? If so, try this:
NSError *error;
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.plist",kAllScoresUnorderedArray]];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:kAllScoresUnorderedArray ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
NSMutableDictionary *test = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
[test setObject:#"bob" forKey:#"tmo"];
[test writeToFile:path atomically: YES];
If not, create the property list in your project.
Related
I just want to replace static plist data to dynamic.this is my code
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docDirPath = [documentsDirectory stringByAppendingPathComponent:#"sample.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath: docDirPath])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath:docDirPath error:&error];
NSLog(#"plist is copied to Directory");
}
i have some data in event array but its not writing/reading can you please suggest me where i am wrong.
NSLog(#"data coming from server....%#",self.eventDataArray);
//Writing array to document directory
NSArray *docpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [docpaths objectAtIndex:0];
NSString *path = [NSString stringWithFormat:#"%#/sample.plist", documentsDirectoryPath];
[self.eventDataArray writeToFile:path atomically:YES];
NSLog(#"plist path string....%#",path);
//reading from document directory
NSMutableArray *readingArray=[[NSMutableArray alloc] initWithContentsOfFile:path];
NSLog(#"plist reading data is here......%#",readingArray);
I write a file to the documents directory of an app:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *file = [documentsDirectory stringByAppendingPathComponent:#"data.json"];
[responseString writeToFile:file atomically:YES];
But how can I access the file?
My code looks like this:
NSString *filePath = [[NSFileManager defaultManager] pathForRessorce: #"data" ofType:#"JSON"];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];
The file path is not populated...can somebody give me a hint?
Thanks!
You are reading from resource folder, not from document directory. You need to populate filePath in the same way that you have used during writing. That is:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"data.json"];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath];
"initwithcontentsoffile is deprecated" warning
So Instead of:
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath];
use:
NSString *userInfoFileContent = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
This gives me...
NSString *filename2 = [NSString stringWithFormat:#"/newFile.json"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#", filename2]];
[self.restClient loadFile:filename2 intoPath:getImagePath];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:getImagePath];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];
...this error:
File loaded into path: (null)
...but why?? this code was under view did load. the file is in dropbox, so what is the problem
I'm looking for a way to fill an NSArray with NSStrings that are the names of the directories in the apps documents directory.
This is what I have so far:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
for (//loop until no more directories)
{
NSString *newDir = [documentsDirectory stringByAppendingPathComponent:path];
//Code for retrieving directory names
[directoriesArray addObject:newDir];
}
I'm not sure what the for loop's conditions need to be and I'm not sure how to retrieve the directories names.
Thanks for the advice in advance.
Try this
NSFileManager *fileManager = [NSFileManager defaultManager];
filesArray = [[fileManager contentsOfDirectoryAtPath:urDocumentsFolderPath error:nil] retain];
for(NSString *file in filesArray) {
//Do something
}
So in your case
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
filesArray = [[fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil] retain];
for(NSString *file in filesArray)
{
NSString *newDir = [documentsDirectory stringByAppendingPathComponent:file];
//Code for retrieving directory names
[directoriesArray addObject:newDir];
}
Hope this helps
"apps documents directory" = ~/Users/User/Applications? NSDocumentsDirectory or NSApplicationDirectory?
Maybe:
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL fileIsDirectory = FALSE;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *content = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:NULL]; //OSX v10.5
NSEnumerator *enumeratorContent = [content objectEnumerator];
NSString *file;
while ((file = [emumeratorContent nextObject])) {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
if (fileManager fileExistAtPath:[documentsDirectory stringByAppendingPathComponent:file] isDirectory:&fileIsDirectory && fileIsDirectory)
[directoriesArray addObject:file];
[pool drain];
Try to use NSDirectoryEnumerator(OSX v.10.0) or NSURL too (OSX v.10.6).
This code works beautifully to save my plist to the documents directory.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.addButtonItem;
NSString *path = [[NSBundle mainBundle] pathForResource:#"TitleArray" ofType:#"plist"];
NSMutableArray *tmpArray = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.titles = tmpArray;
[tmpArray release];
//TESTING NEW CODE FOR SAVING TO DOC DIR
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docDirPath = [documentsDirectory stringByAppendingPathComponent:#"TitleArray.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath: docDirPath])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"TitleArray" ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath:docDirPath error:&error];
NSLog(#"plist is copied to Directory");
}
I have not been able to figure out how to load the plist back to the app!
Can anyone help? Thanks.
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"TitleArray.plist"];
NSArray * myList = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *tmpArray = [myList mutableCopy];
self.titles = tmpArray;
[tmpArray release];
didn't work?
If you are asking how to overwrite a plist included in your app's main bundle, then you should know that it cannot be done. Take a look at this question for more details: Can you update a file in the application bundle?
NSError *error; NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docDirPath = [documentsDirectory stringByAppendingPathComponent:#"TitleArray.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath: docDirPath])
{
docDirPath = [[NSBundle mainBundle] pathForResource:#"TitleArray" ofType:#"plist"];
}
NSMutableArray *tmpArray = [[[NSMutableArray alloc] initWithContentsOfFile: docDirPath] mutableCopy];