I want to store some data of NSMutableArray type to file. I tried the following code but it did not work for me:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"test"];
[self.arrayHit writeToFile:filePath atomically:YES]
One thing I am not sure of is, the path after stringByAppendingPathComponent, I just create an empty file within my project, the structure is like the following:
But after a while, I came across an answer in StackOverflow mentioned that the code above can not preserve NSMutableArray but instead works for NSArray, so I tried to use the suggested code below to preserve NSMutableArray:
BOOL success = [NSKeyedArchiver archiveRootObject:self.arrayHit toFile:@"test"];
but still no luck.
There are two things I am not sure:
1. What file should I create to store the data, since I don't know any, I just created an empty file (I see there are other choices like property list, string file, rich text file etc.) see image below:
2. What path should I put to store the data? If the file is at the same directory of my code, can I just use @"test" directly?