I’ve got this wired problem, I cannot get the content from the file and initiate my NSMutableArray with it.
Here’s my code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"Does file exist?: %i", [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat: @"%@/length.txt", documentsDirectory]]);
NSMutableArray *tempArr;
tempArr = [[NSMutableArray alloc] initWithContentsOfFile:[NSString stringWithFormat: @"%@/length.txt", documentsDirectory]];
When trying this, initWithContentsOfFile returns (null). The row checking if the file exist prints ‘1’ to the console.
This is the code I’m using to save the data:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[length.text writeToFile:[NSString stringWithFormat: @"%@/length.txt", documentsDirectory] atomically:NO];
I’m using more or less exactly the same code in a different program without problems.
Really need some help here, perhaps I’m just blind for the moment…
The docs for NSArray’s
initWithContentsOfFile:method say:You don’t include the declaration of
lengthin your code snippet, but I’m guessing thatlength.textreturns an NSString object, not an NSArray. So you’d need to read that back from a file usinginitWithContentsOfFile:from NSString, not NSArray.