0

I have a one-time need to create a file from an iOS mutable array. The array will be a short animated drawing.

That will be input to an app.

I write the array, and redraw it in the app, so I know that it's getting populated.

I've tried to do something as simple as this:

- (void) writeFile {
//CREATE FILE
NSLog(@"%s", __FUNCTION__);
[writeArray writeToURL:[NSURL fileURLWithPath:[@"~/Desktop/animation.data" stringByExpandingTildeInPath]] atomically:NO];
}

but I must be doing something wrong, as no file appears..

As the file is small (4-8K), maybe I should take a different approach?

Any help will be appreciated.

ICL1901
  • 7,632
  • 14
  • 90
  • 138

1 Answers1

1

Of course this won't work on the device, but I assume you're just trying to work in the Simulator for some kind of testing. fileURLWithPath: doesn't expand ~. You need a full path here. None of the path searching routines is going to point into your user folder in any case, since that doesn't exist on iOS.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • right, I should have said this was running only on the sim.. I will add a full path and get back... Thanks for responding so quickly. In any case, would it be better to just hold the info as an NSDefault dictionary? – ICL1901 Mar 23 '12 at 17:29
  • NSUserDefaults is not designed for large data like an image. But you could also just write to the Documents directory and then access the results from ~/Library/Application Support/iPhone Simulator. Whatever works best for you. – Rob Napier Mar 23 '12 at 17:43
  • Rob, sorry to be a pain - I know how to read/copy a file from the Documents Directory. I cannot get my head on how to write to it from a mutable array.. – ICL1901 Mar 23 '12 at 18:00
  • what i have is this: NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Animation" ofType:@"data"]; [writeArray writeToFile:(NSString *)defaultStorePath atomically:NO]; – ICL1901 Mar 23 '12 at 18:02
  • That is the path to a file called "Animation.data" in your Resources directory, not your Documents directory. http://stackoverflow.com/questions/7440662/how-to-get-all-paths-for-files-in-documents-directory – Rob Napier Mar 23 '12 at 18:15
  • OK. I'm stuck.. I've taken the code from the previous question (thanks!), and now have this: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"%@Animation.data"]; [writeArray writeToFile:(NSString *)folderPath atomically:NO]; } I see the writeToFile routine being called, but I don't see any file being written... I know I'm missing something obvious... – ICL1901 Mar 23 '12 at 18:52
  • This is writing to the Documents directory inside the simulator. Did you look down inside of ~/Library/AppSupport/iPhoneSimulator//...? Remember, iOS is not designed to write to the Mac. If you want a Mac app, just write it for Mac and it'll write to your desktop without trouble. – Rob Napier Mar 23 '12 at 19:26
  • Yep, that's where I would expect to find it, but nothing there.. (~/Library/ApplicationSupport/iPhoneSimulator/5.1/AppName/Documents) – ICL1901 Mar 23 '12 at 19:40
  • You have extra noise in this term: [documentsDirectory stringByAppendingPathComponent:@"%@Animation.data"]. There shouldn't be a %@ here. – Rob Napier Mar 23 '12 at 20:10
  • Hi Rob, last time... I can see from a log that the path is ok:folderPath is: /Users/david/Library/Application Support/iPhone Simulator/5.1/Applications/37D6D453-185F-44DE-848C-DA468307D064/Documents/Animation.data, but nothing gets written.. I made the change you suggested. I really appreciate your taking the time. David – ICL1901 Mar 23 '12 at 21:04
  • Most common reason would be that writeArray is actually nil. Also, check the return. Is it YES? Use writeToURL:options:error: and check the error. – Rob Napier Mar 24 '12 at 16:27
  • Rob, I took a break from this, went back to it last night and checked. You were right- the array was nil. Fixed that and all is well. Thank you for sticking with me... David – ICL1901 Mar 28 '12 at 12:20