The issue is that the getFilePath
method is using NSDocumentationDirectory
rather than NSDocumentDirectory
. Unfortunately, Xcode's auto-complete logic makes it all too easy to pick the wrong one.
Two further suggestions:
You should probably be checking the result of
writeTofile
, perhaps something like:NSArray *array1 = @[@1, @5, @3.423]; BOOL success = [array1 writeToFile:path atomically:YES]; NSAssert(success, @"%s: write failed", __FUNCTION__);
Personally, since I often use this pattern for creating a string that references the path of a file in the
NSDocumentDirectory
directory, I created a code snippet for it which eliminates the opportunity for me to mistype this. It gives me "auto complete" for several lines of code. So, suppose I have the following two lines in my code:NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSString *path = [documentsPath stringByAppendingString:<#filename#>];
Clearly, use whatever code you want, but the key is to use
<#filename#>
placeholder for the parameter tostringByAppendingString
. Then, as described in the Creating a Custom Code Snippet documentation (or see NSHipster's discussion on the topic), you can drag these two lines of code to the snippet library give it a good name (and just as importantly, a good shortcut, I use "documentsPath
" for my shortcut). Now, in the future, I can just start typing "documentsPath
" in my code, and I'll be prompted with these two lines of code.Since I've started using this particular code snippet, I've never made a mistake about accidentally grabbing the wrong value instead of
NSDocumentDirectory
.