0

i wrote below code to write the nsmutablearray to filemanager.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// the path to write file
NSString *arrayFile = [documentsDirectory stringByAppendingPathComponent:arrayName];
[imagesarray writeToFile:arrayFile atomically:YES];

but the file id not creating in NSFilemanager and i am not getting what might be the problem is.

can any one please tell me the reason.

user1300511
  • 91
  • 2
  • 11
  • 1
    what is in arrayFile, how you are creating this? – rishi Apr 24 '12 at 11:30
  • With out more code, error message, no we cant tell you what the reason is. – rckoenes Apr 24 '12 at 11:30
  • @RIP in that array i am adding objects contains of UIImage and Int and while retrieving array contents from filemanager i am not getting any error, it just returning 0 objects – user1300511 Apr 24 '12 at 11:39
  • you need to pass the file path where you want to store this as method definition is - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag – rishi Apr 24 '12 at 11:43
  • @RIP i did that...for your reference i edited my question with the code what i wrote. – user1300511 Apr 24 '12 at 11:46
  • `stringByAppendingPathComponent:arrayName` What you are appending here and whats the resulting path – iDroid Apr 24 '12 at 12:03

1 Answers1

2

There are several things that may cause this piece of code to fail. The things to check are: 1) Make sure that the value of arrayFile is a correct file name in the absolute path of your application sandbox. It cannot in the app bundle or outside the sandbox. 2) Make sure that all the elements in the imagesArray are writable via this method. This method will only write NSString, NSNumber, NSDate, NSData, NSArray, and NSDictionary objects. If the object is an NSDictionary or NSArray then each every element or sub element must also be one of those four types.

The name of the array implies that you're writing an array of images. That will not work because UIImage is not one of these sacred 6 types. You can convert a UIImage to JPEG or PNG representation using a snippet like the following:

UIImage *image = [UIImage imageNamed:@"myimage.png"];
NSData *data = UIImagePNGRepresentation(image, 1.0);
Jack Cox
  • 3,290
  • 24
  • 25
  • Thanks for your reply Jack Cox but in my object i have to add int type also and have to save that in file manager...than what is the solution – user1300511 Apr 24 '12 at 12:32
  • 1
    I just edited my response. The NSArray documentation omitted two types that are allowed NSNumber and NSDate. If you use the [NSNumber numberWithInt:] you can make the array an array of NSNumbers, each containing an int. – Jack Cox Apr 24 '12 at 12:41
  • Create a Dictionary , have the keys "id & values".. Map your int id to the key "id" and array to the key "values" then write the dictionary into a file. – iDroid Apr 24 '12 at 12:43
  • @JackCox so as you said i can create an object of NSDATA and NSNUMBER and i can add that object to array and than save that array to filemanager, this will work fine correct? NOTE: OBJECT Means that is a class of NSObject superclass – user1300511 Apr 24 '12 at 13:00
  • @JackCox i am creating NSDATA and NSNUMEBR in NSOBJECT class and adding that to array and trying to save that array to FleManager, will this work – user1300511 Apr 24 '12 at 13:06
  • I'm don't understand what you're proposing here. Can you post a code snippet with what you're doing. – Jack Cox Apr 24 '12 at 13:34