I am fairly new to archiving an object I have. I have a dictionary that I want to archive.. so first I got the path by doing:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
dictionaryPath = [paths objectAtIndex:0];
so is the next step to append this dictionaryPath with a .txt? Or should I create something first at this directory? What is then the data type?
I want to be able to use it for:
[NSKeyedArchiver archiveRootObject:locationDic toFile:dictionaryPath];
Where dictionaryPath is the path to store this NSDictionary
NSSearchPathForDirectoriesInDomains will give you an array of directory paths. So if you take the first one, you need to append a filename (whatever filename you'd like, with whatever extension you'd like), and then use that path as the location to write out to.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dictionaryFilePath = [documentsDirectory stringByAppendingPathComponent:#"Filename.extension"];
[NSKeyedArchiver archiveRootObject:locationDic toFile:dictionaryFilePath];
Related
I have a strange problem, this code check if a file exist inside document folder:
- (BOOL) checkIfFileExist:(NSString *)path {
NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentsPaths objectAtIndex:0];
NSString *fileDaControllere = [documentsDirectory stringByAppendingString:path];
if ([[NSFileManager defaultManager] fileExistsAtPath:fileDaControllere]) {
NSLog(#"exist");
return YES;
}
else {
NSLog(#"not exist");
return NO;
}
}
the problem is that I get alway file not exist while the file exist (in this case the path is Style.css)! where is the mistake?
The path seems to be correct:
Path: /Users/kikko/Library/Application Support/iPhone Simulator/6.0/Applications/38161AFA-2740-4BE2-9EC4-C5C6B317D270/Documents/Style.css
Here you can see the path on xcode and real path
http://www.allmyapp.net/wp-content/iFormulario/1.png
http://www.allmyapp.net/wp-content/iFormulario/2.png
The problem may be in the fact that you just append the file without with check if there is a path delimitor:
NSString *fileDaControllere = [documentsDirectory stringByAppendingString:path];
Thus you would become something like ../DocumentsStyle.css but it should be ../Documents/Style.css.
NSString has a special method for appending path components stringByAppendingPathComponent:
NSString *fileDaControllere = [documentsDirectory stringByAppendingPathComponent:path];
At the end I solved this issue, the strange think is that I don't know how I solved it,
the first problem is that I pass to checkIfFifFileExist the absolute path while I need to pass it relative path, and the the function trasform it to absolute path (my big big error), after this I think the problem is the use of "/", I delete all and rewrite all the code and I doing some test.
I copy folder from bundle:
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *folderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"/icone"];
NSString *iconePath = [documentsDir stringByAppendingPathComponent:#"/icone"];
[[NSFileManager defaultManager] copyItemAtPath:folderPath toPath:iconePath error:nil];
Then I make the path of my image in this way:
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *imagePath = [documentsDir stringByAppendingPathComponent:self.objMateria.iconaMateria];
and now the file exist, a strange thing is that if:
self.objMateria.iconaMateria = /icona/Home/fisica.png
or
self.objMateria.iconaMateria = icona/Home/fisica.png
nothing change, I see the image, while I think that one of this has a wrong path...
My score is displaying just not in the correct order. I've been trying to make a method that uses #selector(compare:) but have had no such luck.
Here's the code I'm working with and I'm wanting to display it from highest to lowest. I'm also wanting to have it so that if you load the app for the first time it creates an empty array so that if the user tries to look at the highscores it doesn't crash the app.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *scoresListPath = [documentsDirectory stringByAppendingPathComponent:#"scores.plist"];
scoresList = [[NSMutableArray arrayWithContentsOfFile:scoresListPath] retain];
if (scoresList == nil) {
scoresList = [[NSMutableArray array] retain];
}
and
- (void)addHighScore:(float)finalScore {
[scoresList addObject:[NSNumber numberWithFloat:finalScore]];
[scoresList sortUsingSelector:#selector(compare:)];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *scoresListPath = [documentsDirectory stringByAppendingPathComponent:#"scores.plist"];
[scoresList writeToFile:scoresListPath atomically:YES];
}
The reason your code is failing is because arrayWithContentsOfFile: returns an immutable array even though it is called on NSMutableArray. You should make a mutable copy of the array that you read of the file like this,
scoresList = [[NSArray arrayWithContentsOfFile:scoresListPath] mutableCopy];
This will give you an NSMutableArray object which can be added to.
Not sure about lowest but,you can use GKLeaderBoardViewController class for counting Highest Score.For more details I suggested you to read below link.Thanks
http://developer.apple.com/library/ios/#documentation/GameKit/Reference/GKLeaderboardViewController_Ref/Reference/Reference.html
I was trying to use a plist to store an array with the below code:
NSString *name = firstName.text;
NSString *path = [[NSBundle mainBundle] pathForResource:#"Names" ofType:#"plist"];
NSMutableArray *namesArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
[namesArray addObject:name];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[paths release];
NSString *docDirPath = [documentsDirectory stringByAppendingPathComponent:#"Names.plist"];
[namesArray writeToFile:docDirPath atomically:YES];
namesArray = [[NSMutableArray alloc] initWithContentsOfFile:docDirPath];
This code seems to work. Using NSLog, I have found that after this code executes the plist contains what I want it to, however, my program crashes because it generates an EXC_BAD_ACCESS on a device, and on the simulator it just crashes without an explanation. Does anyone know why that might happen?
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES); //Auto-released array
NSString *documentsDirectory = [paths objectAtIndex:0];
[paths release]; //Oh noes!
You don't own the reference to paths, so don't release it. Remove [paths release] and I'll bet you're fine. You're crashing because the autorelease pool is releasing paths after you've already done it yourself.
Quoth the guide:
You only release or autorelease objects you own. You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” ... or if you send it a retain message.
Have you checked, at which place it is giving EXC_BAD_ACCESS error.
In your code there are two wrong things; those are.
The Plist file consists a dictionary not an array, Here in the code you are copying the file data to an array. and saving the array to the plist file.
Second one is you are releasing the "paths" array, with out completion of usage of it. you have to release that array at the end of the statements; like after updating the array to the file.
Regards,
Satya
I retrieve an image I have saved at the documents directory like this:
//set background image
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:#"/myFolder/back.png"];
backgroundImage.image =[UIImage imageWithContentsOfFile:imagePath];
But it is not shown :(
What am I doing wrong?
PD:Yes, it has been saved with the same path and filename (no upper/lower case differences) and yes, I tried opening the image at that path and it works.
Thanks in advance!
It was being done in the wrong place. I was doing it at init instead of viewDidLoad.
I am trying to work directories. Unfortunately i get a non-writeable directory when I run NSSearchPathForDirectoriesInDomains. What I get is:
/Users/me/Library/Application Support/iPhone Simulator/User/Documents
When I run other people's examples I get:
/Users/me/Library/Application Support/iPhone Simulator/User/Applications/6958D21C-C94B-4843-9EF1-70406D0CA3A3/Documents
which is writeable.
The snippet of the code used is
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(documentsDirectory);
What do I need to do allow me to get the same long directory structure?
That long path with the GUID is the documents path for your app, and is expected behavior.
Not sure what your code looks like, but getting the path to your app's document directory should be something like:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
(From Mark/LaMarche p. 331)
since it searches for the "object at index:0"
NSString *documentsDirectory = [paths objectAtIndex:0];
there could be a directory starting with a letter smaller than "d" for documents. Which becomes the "object at index:0". I know this might not be possible but It could be true as well.